Problem
In the C # simulation page landing, where to request a few pages, will initiate the corresponding HTTP request requests, where KeepAlive is set to true, after the request is submitted, then there will be a corresponding response:
RESP = (HttpWebResponse) req. GetResponse ();
Before the multiple debugging, has been able to get the corresponding response, and then read the HTML page.
However, after several times of debugging, without changing the code, the result GetResponse will always time out to die.
"Resolution Process"
1. The default request of timeout is 1000000 milliseconds = 100 seconds, will time out, manually changed to 10 seconds, so it is easier to time out, unable to resolve the problem.
2. Set the HTTP request's keepalive to false and the problem remains.
3. Refer to: C # request. GetResponse (); Timeout problem resolution, and HttpWebRequest multithreading performance problem, request timeout error,
Go to the front total of 4 times httprequest, each increase corresponds to:
RESP = null; 。。。 if (resp! = null) { Resp. Close (); } if (req! = null) { Req. Abort (); } |
The results still didn't solve the problem.
4. Same reference: HttpWebRequest multithreading performance problem, request timeout error,
To try the settings on DefaultConnectionLimit, change to 10:
System.Net.ServicePointManager.DefaultConnectionLimit = 10; |
The problem remains.
5. Again to test the next, about response. Close ()
It didn't solve the problem.
6. Finally inadvertently, simply do not hope, again defaultconnectionlimit set to a larger value of 50:
System.Net.ServicePointManager.DefaultConnectionLimit = 50;
Try it out and solve the problem of timeout.
And then understand why.
Before the default setting of 2, later changed to 10, there is no problem because there are currently many HTTP connections, is not shut down,
And these keepalive connections, are
Because of the code, for the preceding multiple request. It is keepalive to true, and multiple response are not close,
And before debugging many times, so, at this time already exist a lot of alive HTTP connection, already more than 10, so the front set DefaultConnectionLimit 10, also is useless.
Instead of 50, it will suffice.
Summary
The reason for getresponse over here is that there are currently too many alive HTTP connections (greater than 10), so the request for the same HTTP is submitted again, and then GetResponse, it will time out to die.
The solution is to set the DefaultConnectionLimit to a larger value, which is guaranteed to be larger than the number of HTTP connections you currently have alive.
"Experience Summary"
Write the request code for HTTP later, if it is not necessary to keepalive, then set KeepAlive to False:
Req. KeepAlive = false;
And do the corresponding closing action:
if (resp! = null) { Resp. Close (); } if (req! = null) { Req. Abort (); } |
"PostScript 2012-03-01"
And occasionally encountered once, DefaultConnectionLimit is already 200, big enough, but GetResponse and GetRequestStream, or will time out the death of the problem, specifically what causes is not very clear, but after tossing, Reference:
HttpWebResponse ' s GetResponse () hangs and timeouts
In:
Req = (HttpWebRequest) webrequest.create (Constskydriveurl); Setcommonhttpreqpara (ref req); RESP = (HttpWebResponse) req. GetResponse (); |
Before, add a garbage collection:
System.GC.Collect ();
And then solved the GetResponse timeout problem, and the back of the GetRequestStream also can work, no time-out.
So, it looks like the current system because of debugging multiple times, and HttpWebRequest and HttpWebResponse are not normal to close, may be residual some HTTP links, and then may affect the subsequent use of HTTP, after garbage collection, It is estimated that the remaining HTTP-related resources will be released, and then HTTP will work properly.
Summary
For GetResponse or GetRequestStream, the reason for the time-out of death may be:
1.DefaultConnectionLimit is the default of 2, and the current HTTP connection run out, causing subsequent getresponse or getrequeststream timeouts to die
==>> default system only supports simultaneous presence of 2 HTTP connection
==>> using HttpWebRequest, if there is no close, it consumes 1 HTTP connection, so if you use HttpWebRequest more than 2 times without close, Then use the connection of the system's HTTP, then go to the Httpwebrequest,getresponse and die.
Workaround:
Option 1:
Each time you finish using HttpWebRequest, use
?
To close the corresponding HTTP connection
The best corresponding httpwebresponse should also be close:
?
12 |
resp.Close(); resp = null ; |
Method 2:
Change the value of the defaultconnectionlimit to be large enough, for example:
?
1 |
System.Net.ServicePointManager.DefaultConnectionLimit = 200; |
2. HTTP-related resources in the system are not properly released, causing subsequent GetResponse or getrequeststream timeouts to die
As I've encountered here, it could be that the HTTP-related function was called before, and the resource was not completely released properly, causing the defaultconnectionlimit to be large enough, but still dead, at which point a garbage collection would be done before the HTTP request code. Then the subsequent HTTP GetResponse or GetRequestStream is normal, and will not time out to die.
The reference code is as follows:
?
12345 |
System.GC.Collect(); req = (HttpWebRequest)WebRequest.Create(constSkydriveUrl); setCommonHttpReqPara( ref req); resp = (HttpWebResponse)req.GetResponse(); |
3.Http GET request, do not manually set the value of ContentLength
This is the reference here: HttpWebRequest.GetResponse () hangs the second time it is called and recorded here, perhaps someone is this reason, so can be used for reference.
That is, the GET request for HTTP, do not add code similar to the following:
?
12 |
if (m_contentLength > 0) httpWebRequest.ContentLength = m_contentLength; |
Do not manually modify the value of the corresponding contentlength, C # HTTP related library functions, will automatically help you calculate.
Note: In the Post method, it is true to manually populate the data and calculate the data size, and then manually assign a value to the contentlength.
4. Other possible causes
(1) Questions about KeepAlive
If the HTTP request is set to Keepalive=true, then the corresponding HTTP connection will remain connected to the server.
So if none of the above solutions solve the problem of timeouts, try setting KeepAlive to False to see if it can be resolved.
(2) About sleep
Some people seem to have solved this problem by adding the corresponding sleep before the HTTP request. If you need one, you can try it.
(3) Timeout of HttpWebRequest
In general, since the time-out, often due to the wrong use of functions or network problems caused by, so in fact here for some people to the HttpWebRequest of the value of the timeout to change the larger, often is useless.
However, if the network response is slow to cause a timeout, then you can try to change the value of the HttpWebRequest timeout to a larger one.
(where the default value of HttpWebRequest timeout is 100,000 milliseconds ==100 seconds)
Reference code: req.Timeout = 5 * 60 * 1000;
// 5 minutes
Original: http://www.crifan.com/fixed_problem_sometime_httpwebrequest_getresponse_timeout/comment-page-1/#
HttpWebRequest GetResponse or GetRequestStream occasionally timed out + summary of various timeouts and the possible death of the corresponding