[Reprinted] The getresponse or getrequeststream of httpwebrequest occasionally times out + summarizes the possibility of timeout death and Corresponding Solutions

Source: Internet
Author: User

[Problem]

Use C # To simulate web page login. When several pages are requested, the corresponding HTTP request will be initiated, with keepalive set to true. After the request is submitted, the corresponding response will be generated:

Resp = (httpwebresponse) Req. getresponse ();

After debugging multiple times, you can obtain the corresponding response normally and then read the HTML page.

However, the subsequent debugging has not changed.CodeThe getresponse always times out and dies.

 

[Solution process]

1. The default request timeout value is 1000000 milliseconds = 100 seconds, which will time out and be changed to 10 seconds manually. Therefore, it is easier to time out and the problem cannot be solved.

2. Set the keepalive of the HTTP request to false. the problem persists.

3. For details, refer to: C # request. getresponse (); timeout problem solving, and httpwebrequest multi-thread performance problem, request timeout error,

Go to the previous four httprequest requests and add the corresponding one each time:

Resp = NULL;
...
If (RESP! = NULL)
{
Resp. Close ();
}
If (req! = NULL)
{
Req. Abort ();
}

The problem persists.

4. For details, refer to: httpwebrequest multithreading performance problems and request timeout errors,

Try Setting defaultconnectionlimit to 10:

System. net. servicepointmanager. defaultconnectionlimit = 10;

The problem persists.

5. I went to test again, about response. Close ()

The problem is not solved.

6. In the end, inadvertently, simply do not hold hope, and then set defaconnecticonnectionlimit to a larger value of 50:

System. net. servicepointmanager. defaultconnectionlimit = 50;

After trying, the problem of timeout is solved.

Then you can understand the cause.

Previously, the default value is 2, and then changed to 10, which does not solve the problem because there are currently many HTTP connections that have not been closed,

The keepalive connections are

Because the Code contains multiple previous requests. Keepalive is true, and multiple response is not close,

Debugging has been performed many times before. Therefore, there are already many alive HTTP connections and more than 10 connections. Therefore, we set defaconnecticonnectionlimit to 10, it is useless.

It is enough to change to 50.

[Summary]

The reason getresponse exceeded here is that there are too many alive HTTP connections (more than 10) Currently, so submitting the same HTTP request again and then getresponse will time out and die.

The solution is to set defaultconnectionlimit to a relatively large value, which must be greater than the number of HTTP connections of your existing alive instance.

[Experience Summary]

In the future, write the HTTP Request Code. If you do not need keepalive, set keepalive to false:

Req. keepalive = false;

And the corresponding ending action:

If (RESP! = NULL)
{
Resp. Close ();
}
If (req! = NULL)
{
Req. Abort ();
}

 

[Postscript]

Occasionally, defaultconnectionlimit is already 200, which is large enough. However, getresponse and getrequeststream will still time out and die. It is not clear what causes it, however, after a long journey, refer:

Httpwebresponse's getresponse () hangs and timeouts

In:

Req = (httpwebrequest) webrequest. Create (constskydriveurl );
Setcommonhttpreqpara (ref req );
Resp = (httpwebresponse) Req. getresponse ();

Before that, add a garbage collection statement:

System. gc. Collect ();

Then the getresponse timeout problem is solved, and the subsequent getrequeststream can also work normally without timeout.

Therefore, it seems that the current system has been debugged for multiple times, and both httpwebrequest and httpwebresponse have not been properly closed, and some HTTP links may be left, the subsequent use of HTTP may be affected. After garbage collection, it is estimated that the residual http-related resources will be released, and then HTTP will work normally.

 

[Summary]

The cause of getresponse or getrequeststream timeout may be:

1. defaultconnectionlimit is the default 2, and the current HTTP connection is used up, resulting in the subsequent getresponse or getrequeststream timeout to die

==>> By default, only two HTTP connections exist simultaneously.

==>> If no close is found after httpwebrequest is used, one HTTP connection will be used. Therefore, if httpwebrequest is used more than two times but not close, the system's HTTP connection will be used up, then use httpwebrequest, and getresponse will die.

Solution:

Method 1:

Httpwebrequest is used

?
12 Req. Close ();Req =Null;

Close the corresponding HTTP Connection

It is best to close the corresponding httpwebresponse:

?
12 Resp. Close ();Resp =Null;

Method 2:

Modify the defaultconnectionlimit value to be large enough, for example:

?
1 System. net. servicepointmanager. defaultconnectionlimit = 200;

 

2. the http-related resources in the system are not correctly released, resulting in the subsequent getresponse or getrequeststream timeout to die.

As I encountered here, it may have been because the relevant HTTP functions were called and the resources were not completely released. As a result, although defaconnectionlimit is large enough, it will still die, in this case, a garbage collection is performed before the HTTP Request Code, and the subsequent HTTP getresponse or getrequeststream will be normal and will not expire.

The reference code is as follows:

?
12345 System. gc. Collect (); Req = (httpwebrequest) webrequest. Create (constskydriveurl );Setcommonhttpreqpara (Ref REQ );Resp = (httpwebresponse) Req. getresponse ();

 

3. Do not manually set the contentlength value for http get requests.

For more information, see httpwebrequest. getresponse () hangs the second time it is called.

That is, the http get request,NoAdd code similar to the following:

?
12 If (M_contentlength> 0)Httpwebrequest. contentlength = m_contentlength;

Do not manually modify the value of the corresponding contentlength. The HTTP-related library function of C # will automatically help you calculate the value.

Note: In the POST method, you must manually fill in the data and calculate the data size, and then manually assign a value to contentlength.

 

4. other possible causes

(1) keepalive Problems

If keepalive is set to true for an HTTP request, the corresponding HTTP connection will be connected to the server.

If the above method cannot solve the timeout problem, you can try setting keepalive to false to see if it can be solved.

(2) about sleep

Some people seem to have solved this problem by adding the corresponding sleep before the HTTP request. You can also try it.

(3) Timeout of httpwebrequest

In general, since timeout is often caused by incorrect use of functions or network problems, in fact, for some people to change the timeout value of httpwebrequest to a greater value, it is often useless.

However, in case of timeout due to slow network response, you can try to change the timeout value of httpwebrequest to a greater value.

(The default timeout value of httpwebrequest 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.