两次服务器500错误后,C#HttpWebRequest超时

在我生成两个由于“(500)内部服务器错误500”而引发exception的C#HttpWebRequests之后,第三次尝试抛出超时exception。 为什么不抛出另一个(500)内部服务器错误exception?

当我重新启动我的应用程序时,它会抛出两个500错误,然后再次开始超时。

这是我的代码:

GetPages GetPages = new GetPages(); string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */ GetPages.Dispose(); GetPages GetPages = new GetPages(); string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */ GetPages.Dispose(); GetPages GetPages = new GetPages(); string test = GetPages.GetPage(); /* Exception: time out, why? */ GetPages.Dispose(); 

这是GetPages类和GetPage方法:

 namespace MyNamespace { class GetPages { public string GetPage() { this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl"); try { StringBuilder postData = new StringBuilder(100); postData.Append("test=test"); byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString()); httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); httpRequest.KeepAlive = false; httpRequest.Proxy = null; httpRequest.Method = "POST"; httpRequest.Timeout = 10; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.ContentLength = dataArray.Length; using (this.requestStream = httpRequest.GetRequestStream()) { requestStream.Write(dataArray, 0, dataArray.Length); requestStream.Flush(); requestStream.Close(); this.webResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8); String responseString = responseReader.ReadToEnd(); MessageBox.Show(responseString); return responseString; } } catch (Exception e) { MessageBox.Show(e.Message); return "FAIL"; } } public void Dispose() { System.GC.SuppressFinalize(this); } } } 

UPDATE

谢谢大家的帮助。 我无法解决这个问题。

处置方法现在消失了。

我已经使用了HttpWebRequest httpRequest,HttpWebResponse webResponse和Stream requestStream local,并使用以下using语句:

 using (webResponse = (HttpWebResponse)httpRequest.GetResponse()) { using (Stream responseStream = webResponse.GetResponseStream()) { using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8)) { responseString = responseReader.ReadToEnd(); } } } 

另一个更新

这是现在的整个GetPage方法:

 public string GetPage() { HttpWebRequest httpRequest; HttpWebResponse webResponse; Stream requestStream; try { StringBuilder postData = new StringBuilder(100); postData.Append("test=test"); byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString()); httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl"); httpRequest.Proxy = null; httpRequest.Method = "POST"; httpRequest.Timeout = 10; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.ContentLength = dataArray.Length; using (requestStream = httpRequest.GetRequestStream()) { /* this is never reached when the time out exception starts so the error seems to be related to too many GetRequestStreams */ requestStream.Write(dataArray, 0, dataArray.Length); webResponse = (HttpWebResponse)httpRequest.GetResponse(); /* this is never reached when the 500 server error occurs */ String responseString = ""; using (Stream responseStream = webResponse.GetResponseStream()) { using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8)) { responseString = responseReader.ReadToEnd(); return responseString; } } } } catch (Exception e) { return "FAIL"; } return "..."; } 

** 解决了!! ** httpRequest没有得到中止()。 在catch()块中我添加了httpRequest.abort(),现在它可以正常工作。

怀疑这是问题所在:

 this.webResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8); String responseString = responseReader.ReadToEnd(); 

你没有处理任何这些一次性物品。 这意味着将保持与Web服务器的连接直到完成,因此连接池(每个服务器两个连接)将不允许任何其他请求。

我建议:

  • 您将GetResponse调用移出请求流的using语句
  • 您删除了对请求流的显式调用FlushClose (处理它已经足够了)
  • 你创建webResponsewebRequest局部变量而不是实例变量,除非你以后真的需要它们,在这种情况下你应该在Dispose方法中处理它们。
  • 您可以using WebResponseStreamStreamReader using语句。 (最后一个并不是绝对必要的,因为处理流足够了。)
  • 除非你确实需要,否则你使GetPages不实现IDisposable

HTTP协议定义只能同时向同一服务器建立两个连接。 成功或不成功读取后关闭responseStream