HttpWebRequest GetResponse or GetRequestStream occasional timeout + summary of various timeouts and the possible and corresponding solutions

Source: Internet
Author: User

"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 corresponding RESPONSE:RESP=(HttpWebResponse) req. GetResponse (), the previous debugging, has always 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"1The default request 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. 2set the keepalive of HTTP request to false, the problem remains. 3. To reference: C # request. GetResponse (); Time-out problem resolution, and HttpWebRequest multithreading performance problems, request time-out errors, go to the front total of 4 times the HttpRequest, each increase corresponds to: Resp=NULL;... if(Resp! =NULL) {resp. Close (); } if(req! =NULL) {req. Abort (); The result still doesn't solve the problem. 4also refer to: HttpWebRequest multithreading performance problem, request timeout error HttpWebRequest Multithreading performance problem, request timeout error, go to try about DefaultConnectionLimit settings, Change to 10:system.net.servicepointmanager.defaultconnectionlimit=Ten, the problem remains. 5and went to test the next, about response. Close () also does not solve the problem. 6. Finally inadvertently, simply do not hope, again defaultconnectionlimit set to a larger value 50:system.net.servicepointmanager.defaultconnectionlimit = -and tried, the result is to solve the problem of time-out. And then understand why. Before the default setting of 2, later changed to 10, there is no solution to the problem is that there are currently many HTTP connections, is not closed, and these keepalive connections, are due to the code, for the preceding multiple request. It is keepalive to true, and more than one response is not close, and has been debugged many times before, so, there are already a lot of alive HTTP connection, has exceeded 10, So the previous set of DefaultConnectionLimit is 10, it is still useless. Instead of 50, it will suffice. "Summary" here getresponse more than the reason is that there are too many alive HTTP connections (more than 10), so submit the same HTTP request again, then go to GetResponse, 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" after writing HTTP request code, if not necessary to KeepAlive, then set KeepAlive to False:req.KeepAlive=falseand do the corresponding closing action:if(Resp! =NULL) {resp. Close (); } if(req! =NULL) {req. Abort (); } "PostScript --Geneva- on"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, refer to: HttpWebResponse ' s GetResponse () hangs and timeouts in: Req=(HttpWebRequest) webrequest.create (Constskydriveurl); Setcommonhttpreqpara (refreq); Resp=(HttpWebResponse) req. GetResponse (); Before, add a garbage collection: System.GC.Collect (), and then solve the GetResponse time-out problem, and the back of the GetRequestStream also can work, do not 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. The "Summary" causes for the GetResponse or GetRequestStream timeout to die may be:1. DefaultConnectionLimit is the default of 2, and the current HTTP connection run out, causing subsequent getresponse or getrequeststream timeouts to die==>>the default system supports only 2 simultaneous HTTP connection==>>If you do not have close after using HttpWebRequest, you will consume 1 HTTP connection, so if you use HttpWebRequest more than 2 times without close, the connection of the system's HTTP will be exhausted. , and then use Httpwebrequest,getresponse to die. Solution: Method 1: Use Req each time you finish using HttpWebRequest. Close (); Req=NULLto close the corresponding HTTP connection the best corresponding HttpWebResponse also to Close:resp.Close (); RESP=NULL; Method 2: Modify the value of the defaultconnectionlimit to be large enough, for example: System.Net.ServicePointManager.DefaultConnectionLimit= $; 2The HTTP-related resources in the system are not properly freed, causing subsequent getresponse or getrequeststream timeouts to die as I have encountered here, possibly by invoking an HTTP-related function without properly releasing the resource correctly. Cause although the defaultconnectionlimit is large enough, but still will die, at this time before the HTTP request code to do a garbage collection, the subsequent HTTP GetResponse or GetRequestStream is normal, Would not have timed out to die. The reference code is as follows: System.GC.Collect (); Req=(HttpWebRequest) webrequest.create (Constskydriveurl); Setcommonhttpreqpara (refreq); 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 iscalled and recorded here, perhaps someone is this reason, so for reference. That is, the GET request for HTTP, do not add code similar to the following:if(M_contentlength >0) Httpwebrequest.contentlength=m_contentlength; Do not go to manually modify the corresponding contentlength value, 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 (1questions 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. (2about 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. (3HttpWebRequest Timeout 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 timeout value 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 timeout for HttpWebRequest is themilliseconds = = -seconds) Reference code: Req. Timeout=5* -* +;//5 minutes

HttpWebRequest GetResponse or GetRequestStream occasional timeout + summary of various timeouts and the possible and corresponding solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.