In HttpWebRequest, GetResponse or GetRequestStream occasionally times out, or some solutions that result from various operation timeouts,

Source: Internet
Author: User

In HttpWebRequest, GetResponse or GetRequestStream occasionally times out, or some solutions that result from various operation timeouts,

Today, it took nearly one day to find out the problem. Baidu kept searching for the cause of the test and found that the solution was very simple, but it would be better to solve it all, record it here, hoping to help you

 payload = System.Text.Encoding.UTF8.GetBytes(postDataStr);                request.ContentLength = payload.Length;                Stream writer = request.GetRequestStream();                writer.Write(payload, 0, payload.Length);                //writer.Flush();                writer.Close();                //HttpWebResponse response = (HttpWebResponse)request.GetResponse();                HttpWebResponse res;                try                {                    System.Net.ServicePointManager.DefaultConnectionLimit = 200;                    res = (HttpWebResponse)request.GetResponse();                }                catch (WebException ex)                {                    res = (HttpWebResponse)ex.Response;                }

Today, my res has always reported operation timeout. Baidu has many solutions for one-to-one testing. To sum up, if you have such an error, try the solution at a time.

First, increase the timeout time of the request. The default value is 100 seconds. (I tested many solutions and found that the timeout time is too short, resulting in operation timeout)

Second, Set request. KeepAlive = false. The reason is that the default KeepAlive attribute is true. Set this attribute to true to send the Connection HTTP header with the Keep-alive value. The application uses KeepAlive to indicate the preference of persistent connections. When the KeepAlive attribute is true, the application establishes a persistent connection with the server that supports them. Remember to close the corresponding closing work in time.

if (request != null)                {                    request.Abort();                }                if (res != null)                {                    res.Close();                }

 

Third:

System.Net.ServicePointManager.DefaultConnectionLimit = 200;

In addition, because the default setting is 2, when there are more than 2 http connections, this operation timeout error will also be reported.

Fourth:

                System.GC.Collect();                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);                request.Method = "POST";                request.ContentType = "application/x-www-form-urlencoded";                request.ContentLength = postDataStr.Length;                request.KeepAlive = false;                request.Timeout = 6000000;

In

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

Added: System. GC. Collect ();

The reason is: the http-related resources in the system are not correctly released, resulting in the subsequent GetResponse or GetRequestStream timeout to die.

 

All codes after improvement:

public static bool HttpPost(string Url, string postDataStr, out string errMsg, out string returnData)        {            try            {                System.GC.Collect();                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);                request.Method = "POST";                request.ContentType = "application/x-www-form-urlencoded";                request.ContentLength = postDataStr.Length;                request.KeepAlive = false;                request.Timeout = 6000000;                //StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);                //writer.Write(postDataStr);                //writer.Flush();                byte[] payload;                payload = System.Text.Encoding.UTF8.GetBytes(postDataStr);                request.ContentLength = payload.Length;                Stream writer = request.GetRequestStream();                writer.Write(payload, 0, payload.Length);                //writer.Flush();                writer.Close();                //HttpWebResponse response = (HttpWebResponse)request.GetResponse();                HttpWebResponse res;                try                {                    System.Net.ServicePointManager.DefaultConnectionLimit = 200;                    res = (HttpWebResponse)request.GetResponse();                }                catch (WebException ex)                {                    res = (HttpWebResponse)ex.Response;                }                                              //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);                StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);                string retString = reader.ReadToEnd();                errMsg = "";                try                {                    returnData = Util.base642Str(retString);                }                catch (Exception)                {                    returnData = retString;                    //throw;                }                if (request != null)                {                    request.Abort();                }                if (res != null)                {                    res.Close();                }                return true;            }

 

Reference: https://www.cnblogs.com/wohexiaocai/p/5673193.html

Related Article

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.