"Resolved" https request--An error occurred when the underlying connection was sent off

Source: Internet
Author: User

I use a third-party push service when I do push messaging for commercial projects. Here to avoid the suspicion of advertising, will not report the name. Because it is through the call API interface, so the Post method is written by itself, but in the development environment can be normal push, but on the line on a variety of problems. Landlord speculation may be the development of the environment test, push the message is relatively small, and the number of online push messages, resulting in connection with the number of errors related. The following is helpful and is documented here.

The error of the report is: 1. "The underlying connection has been closed: An error occurred while sending";

October 25, 2016 18:56:53 update
Later, all the methods in this article have been tried and found that the problem is not solved at last. Finally the problem finally solved, the solution is still Google out:

The previous wording:

Can solve the problem of the wording:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Analysis: Because the requested URL is HTTPS-based, the POST request must be added ServicePointManager.SecurityProtocol . But which protocol to choose? Start to see the project is based on SSL, simply also use SSL, but no effect, and finally directly to the Protocol enumeration or the form of all written out, success.

Transferred from: Http://www.crifan.com/fixed_problem_sometime_httpwebrequest_getresponse_timeout

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. The previous total of 4 times HttpRequest, each time the corresponding code is added, the result is still not solve the problem.

null; if (resp != null) {     resp.Close(); } if (req != null) { req.Abort(); }

4. To try the settings on DefaultConnectionLimit, change to 10:
System.Net.ServicePointManager.DefaultConnectionLimit = 10;The problem remains.

5. Again to test the next, close response.Close() also did not solve the problem.

    1. Finally inadvertently, simply not hopeful, 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, and then to 10, there is no solution to the problem is that there are currently many HTTP connections, are not closed, and these connections are keepalive, because 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

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(); }
    1. And occasionally encountered once, DefaultConnectionLimit is already 200, big enough, but GetResponse and Getrequest and Stream are released, 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(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 HTTP connection of the system, and then go to using Httpwebrequest,getresponse will die.

Workaround:

Option 1:

Each time you finish using HttpWebRequest, use

req.Close();req=null;

To close the corresponding HTTP connection.

The best corresponding httpwebresponse should also be close:

resp.Close();resp = null;

Method 2:

Change the value of the defaultconnectionlimit to be large enough, for example:

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:

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:

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

"Resolved" https request--An error occurred when the underlying connection was sent off

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.