编程语言
首页 > 编程语言> > C#-HttpWebRequest仅在使用POST模式时获取404页面

C#-HttpWebRequest仅在使用POST模式时获取404页面

作者:互联网

首先:我知道这个问题被问了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)
      {

      }
   }

标签:windows-phone-8,windows-phone,windows-phone-7,http-post,c
来源: https://codeday.me/bug/20191121/2055112.html