HttpWebRequest GetResponse or GetRequestStream time-out method

Source: Internet
Author: User
Tags garbage collection

Transfer from http://www.crifan.com/fixed_problem_sometime_httpwebrequest_getresponse_timeout/comment-page-1/#comment-436221

The reason for the death of the GetResponse or GetRequestStream timeout may be:

1.DefaultConnectionLimit is the default of 2, and the current HTTP connection is run out, causing subsequent getresponse or GetRequestStream to die

==>> default system only supports 2 HTTP connection

==>> using HttpWebRequest, if not close, consumes 1 HTTP connection, so if you use HttpWebRequest more than 2 times without close, Then use the HTTP connection of the system and then go to the Httpwebrequest,getresponse and die.

Solution:

Option 1:

Use the HttpWebRequest every time you use it?

1 2 Req. Close (); req= null;

To turn off the corresponding HTTP connection

The best corresponding HttpWebResponse also want close:?

1 2 Resp. Close (); RESP = null;

Method 2:

Change the value of 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 to die out

As I have encountered here, it may be that the HTTP correlation function was previously invoked, and the resource was not fully released properly, causing the defaultconnectionlimit to be large enough to die, and then to do a garbage collection before the HTTP request code, Then the subsequent HTTP GetResponse or GetRequestStream is normal, will not time out dead.

The reference code is as follows:?

1 2 3 4 5 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 the ContentLength

This is the reference here: HttpWebRequest.GetResponse () hangs the second time it is called and it is recorded here, may be the reason, so for reference.

That is, HTTP GET requests, do not add code similar to the following:?

1 2 if (M_contentlength > 0) httpwebrequest.contentlength = m_contentlength;

Do not 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 size of the data, and then manually assign values to the ContentLength.

4. Other possible causes

(1) on the issue of 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 solves the problem of timeout, try setting KeepAlive to False to see if it can be resolved.

(2) About sleep

Some people seem to have solved the problem by adding a corresponding sleep before the HTTP request. People who need it, you can try it.

(3) The timeout of HttpWebRequest

Generally speaking, since the timeout, often due to the wrong use of functions or network problems caused, so in fact here for some people to the value of the HttpWebRequest of the timeout of the larger, often is useless.

Just in case of a timeout due to slow network response, you can try to change the value of the HttpWebRequest timeout to larger.

(where HttpWebRequest's timeout default value is 100,000 milliseconds ==100 seconds)

Reference code:?

1 Req. Timeout = 5 * 60 * 1000; 5 minutes

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.