有谁知道为什么我收到的HttpWebRequest超时? [英] Does anyone know why I receive an HttpWebRequest Timeout?

查看:148
本文介绍了有谁知道为什么我收到的HttpWebRequest超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果你能帮助我,我是个有一个错误。我有我创建了一个HTTP管理器,可以帮助我处理发布/从网站上歌厅的数据。它一直很好,直到最近,当我试图用两者的混合。第一循环轮一切正常,在第二圈就挂在HttpWebRequest.GetRequestStream()。我已阅读所有过网,并没有发现任何真正的解决方案。下面是为取/接收代码块:

I was wondering if you can help me with a bug I ma having. I have a HTTP Manager I have created that helps me dealing with POSTing/GETing data from websites. It has worked fine until recently when I am trying to use a mixture of both. First loop round everything works, on the second loop it hangs on HttpWebRequest.GetRequestStream(). I have read all over the net and have found no real solution. Below are the codeblocks for the fetching/receiving:

 ASCIIEncoding encoding = new ASCIIEncoding();
 byte[] buffer = encoding.GetBytes(_PostData);

_HttpWebRequest = (HttpWebRequest)WebRequest.Create(_FetchUrl);
_HttpWebRequest.Credentials = _Credentials;
_HttpWebRequest.Method = _RequestType.ToString();
_HttpWebRequest.ContentType = "application/x-www-form-urlencoded";
_HttpWebRequest.ContentLength = buffer.Length;
_HttpWebRequest.UserAgent = userAgent;
_HttpWebRequest.CookieContainer = _CookieContainer;
_HttpWebRequest.KeepAlive = false;
_HttpWebRequest.AllowAutoRedirect = _AllowAutoRedirect;
_HttpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
_HttpWebRequest.ServicePoint.Expect100Continue = false;  

 if (_RequestType.Equals(RequestTypes.POST))
{
     // Write POST
 Stream reqStream = _HttpWebRequest.GetRequestStream();
 {
  reqStream.Write(buffer, 0, buffer.Length);
  reqStream.Flush();
  reqStream.Close();
    }
}

和的的回复:

HttpWebResponse httpWebResponse = (HttpWebResponse)_HttpWebRequest.GetResponse();
{
  Stream responseStream = httpWebResponse.GetResponseStream();
  {
    if (_UseGzip)
    {
      if (httpWebResponse.ContentEncoding.ToLower().Contains("gzip"))
      {
        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
      }
      else
      {
        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
      }
    }

    if (responseStream != null)
    {
      StreamReader streamReader = new StreamReader(responseStream);
      {
        try
        {
          _PageContent = streamReader.ReadToEnd();
        }
        finally
        {
          streamReader.Close();
          responseStream.Close();
          httpWebResponse.Close();
        }
      }
    }
    else
    {
      _PageContent = string.Empty;
    }
  }
}
_HttpWebRequest.Abort();



任何人都可以看到任何破绽,为什么我的代码是挂?所有的数据流都被关闭,我已经设置了允许连接到100,我不明白这是为什么打破。

Can anyone see any flaws to why my code is hanging? All streams are being closed, I have set the allowed connections to over 100, I don't understand why this is breaking.

推荐答案

这可能是由于这样的事实,你不处理你的WebResponse类或流或StreamReaders的:

This may be due to the fact that you are not disposing of your WebResponse or streams or StreamReaders:

var request = WebRequest.Create(...);
using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        using (var reader = new StreamReader(responseStream))
        {
            // use the reader
        }
    }
}

这篇关于有谁知道为什么我收到的HttpWebRequest超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆