10

我想知道您是否可以帮助我解决我遇到的错误。我创建了一个 HTTP 管理器,可以帮助我处理来自网站的 POST/GET 数据。直到最近,当我尝试将两者混合使用时,它一直运行良好。第一个循环一切正常,第二个循环挂在 HttpWebRequest.GetRequestStream() 上。我已经阅读了整个网络,并没有找到真正的解决方案。以下是获取/接收的代码块:

 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,我不明白为什么会这样。

4

3 回答 3

11

这可能是因为您没有处理 WebResponse 或流或 StreamReaders:

var request = WebRequest.Create(...);
using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        using (var reader = new StreamReader(responseStream))
        {
            // use the reader
        }
    }
}
于 2010-02-21T07:19:53.090 回答
2

我有同样的问题。我使用这些 using 块正确关闭(处置)了所有流和 HttpWebResponse。当我向 ThreadAbortExceptions 中止的垃圾邮件发送请求时,问题仍然存在。最后,当 ThreadAbortException 发生时,它有助于调用 myWebRequest.Abort()!希望这可以帮助。

于 2011-06-22T12:30:43.867 回答
0

我看到你使用:

HttpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;

配合手动解压方式:

      if (httpWebResponse.ContentEncoding.ToLower().Contains("gzip"))
      {
        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
      }
      else
      {
        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
      }

如果它真的很重要,没有尝试,但我只使用手动方法和类似的代码,它工作正常。实际上我有挂起的问题,但只有一个域,对属性的试验显示出差异。

哦,我怀疑如果你不使用任何后期数据压缩,你需要删除内容编码头

于 2013-11-27T07:58:56.800 回答