欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HttpWebRequest中GetResponse或者说GetRequestStream偶尔超时,或者是各种操作超时造成的假死的一些解决方案

程序员文章站 2022-03-18 14:40:03
今天用了将近一天的时间来查找这个问题的存在,不停的百度查找原因测试原因,发现解决方案很是简单,不过最好还好哦啊都解决了,在这里纪录一下,希望可以帮到你们 今天我的res 一直报操作超时,百度了很多方案,一一测试,总结来说,如果你也出现了这种错误,可以一次尝试一下方案 第一种:加长request的ti ......

今天用了将近一天的时间来查找这个问题的存在,不停的百度查找原因测试原因,发现解决方案很是简单,不过最好还好哦啊都解决了,在这里纪录一下,希望可以帮到你们

 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;
                }

 今天我的res 一直报操作超时,百度了很多方案,一一测试,总结来说,如果你也出现了这种错误,可以一次尝试一下方案

第一种:加长request的timeout的时间,默认是100秒(我就是测试了好多方案,才发现我的timeout的时间太短,引起的操作超时)

第二种:设置request.KeepAlive = false;原因是:默认KeepAlive的属性是true,将此属性设置为 true 以发送带有 Keep-alive 值的 Connection HTTP 标头。 应用程序使用 KeepAlive 指示持久连接的首选项。 当 KeepAlive 属性为 true 时,应用程序与支持它们的服务器建立持久连接。也要记住要及时关闭相对应的关闭工作

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

 

第三种

System.Net.ServicePointManager.DefaultConnectionLimit = 200;

 加上这句话,因为这个默认设置的是2,当http的连接超过2条时,也会报这种操作超时的错误

第四种

                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;

 在

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

 之前加上:System.GC.Collect();

原因是:系统中的http相关的资源没有正确的释放,导致后续的GetResponse或者说GetRequestStream超时死掉

 

    改进之后的所有代码:

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;
            }

 

                       参考文章:https://www.cnblogs.com/wohexiaocai/p/5673193.html