C#-HttpWebRequest仅在使用POST模式时获取404页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-HttpWebRequest仅在使用POST模式时获取404页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3159字,纯文字阅读大概需要5分钟。
内容图文
![C#-HttpWebRequest仅在使用POST模式时获取404页面](/upload/InfoBanner/zyjiaocheng/660/9aca2b812b604010a07ab7f0b1eac8f7.jpg)
首先:我知道这个问题被问了100多次,但是其中大多数问题都是由于超时问题,不正确的网址或通过伪造关闭流而引起的.(相信我,我尝试了所有示例,但都没有工作).
所以,现在我的问题是:在Windows Phone应用程序中,我正在使用HttpWebRequest将一些数据发布到php Web服务.然后,该服务应将数据保存在某些目录中,但是为了简化它,此刻,它仅回显“ hello”.
但是,当我使用以下代码时,我总是得到一个带有404 html HTML文档的404完整文档.因此,我认为我可以排除超时的可能性.似乎请求已到达服务器,但由于某种原因,返回了404.但是真正令我惊讶的是,如果我使用get请求,那么一切都很好.所以这是我的代码:
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.CreateHttp(server + "getfeaturedpicture.php?randomparameter="+ Environment.TickCount);
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
webRequest.Method = "POST";
webRequest.ContentType = "text/plain; charset=utf-8";
StreamWriter writer = new StreamWriter(await Task.Factory.FromAsync<Stream>(webRequest.BeginGetRequestStream, webRequest.EndGetRequestStream, null));
writer.Write(Encoding.UTF8.GetBytes("filter=" + Uri.EscapeDataString(filterML)));
writer.Close();
webRequest.BeginGetResponse(new AsyncCallback((res) =>
{
string strg = getResponseString(res);
Stator.mainPage.Dispatcher.BeginInvoke(() => { MessageBox.Show(strg); });
}), webRequest);
尽管我不认为这是原因,但这是getResponseString的来源:
public static string getResponseString(IAsyncResult asyncResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse webResponse;
try
{
webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
}
catch (WebException ex)
{
webResponse = ex.Response as HttpWebResponse;
}
MemoryStream tempStream = new MemoryStream();
webResponse.GetResponseStream().CopyTo(tempStream);
tempStream.Position = 0;
webResponse.Close();
return new StreamReader(tempStream).ReadToEnd();
}
解决方法:
经过测试的代码在Post方法中可以正常工作.愿这给您一个想法.
public void testSend()
{
try
{
string url = "abc.com";
string str = "test";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "text/plain; charset=utf-8";
req.BeginGetRequestStream(SendRequest, req);
}
catch (WebException)
{
}
}
//Get Response and write body
private void SendRequest(IAsyncResult asyncResult)
{
string str = "test";
string Data = "data=" + str;
HttpWebRequest req= (HttpWebRequest)asyncResult.AsyncState;
byte[] postBytes = Encoding.UTF8.GetBytes(Data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
request.BeginGetResponse(SendResponse, req);
}
//Get Response string
private void SendResponse(IAsyncResult asyncResult)
{
try
{
MemoryStream ms;
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
HttpWebResponse httpResponse = (HttpWebResponse)response;
string _responestring = string.Empty;
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
_responestring = reader.ReadToEnd();
}
}
catch (WebException)
{
}
}
内容总结
以上是互联网集市为您收集整理的C#-HttpWebRequest仅在使用POST模式时获取404页面全部内容,希望文章能够帮你解决C#-HttpWebRequest仅在使用POST模式时获取404页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。