System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()


System.Net.WebException: The operation has timed out  at System.Net.HttpWebRequest.GetResponse()

在請求獲取響應結果的時候,超時,具體原因可能就是如下面Jon Skeet所說,

WebResponse implements IDisposable,
so you should use a using statement for it (and for the StreamReader you create from the stream).
If you leave a WebResponse open, it will take up a connection from the connection pool to that host,
and you can end up with timeouts this way.
WebResponse繼承IDisposable接口,會釋放非托管資源
所以你需要使用聲明來創建資源對象
如果你打開WebResponse資源響應,那么他將通過連接池連接主機,使用這個方式,超時將不會成為問題。
應該是這么翻譯吧╮(╯_╰)╭

This will close the stream and the response even if an exception is thrown, 
so you'll always clean up the resources (in this case releasing the connection back to the pool) promptly.
這個將會關閉stream和response,即使出現拋出異常的情況
所以你總能夠即時清理好資源(釋放應用池的連接)

這個方式,嘗試了一下,暫時沒出現什么問題,算解決了99%吧,等到以后再出現什么問題,再來看。

下面是網上找到的一個解決方案:

http://stackoverflow.com/questions/15493321/system-net-webexception-the-operation-has-timed-out-on-httpwebresponse

Author:Jon Skeet

System.Net.WebException: The operation has timed out on HttpWebResponse

This may well be the problem:

HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();

WebResponse implements IDisposable, so you should use a using statement for it (and for the StreamReader you create from the stream). If you leave a WebResponse open, it will take up a connection from the connection pool to that host, and you can end up with timeouts this way. The fixed code would look like this:

string responseString;
using (var response = myReq.GetResponse())
{
    using (var reader = new StreamReader(response.GetResponseStream())
    {
        responseString = reader.ReadToEnd();
    }
}

This will close the stream and the response even if an exception is thrown, so you'll always clean up the resources (in this case releasing the connection back to the pool) promptly.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM