基础连接已关闭解决办法

      最近微信公众号功能莫名其妙的出问题,在调腾讯和百度接口就出问题,也不知道哪里抽风,只要调用外部接口,POST或者GET提交,准备出错。提示基础连接已关闭......

     httpWebRequest请求错误,基础连接已经关闭: 连接被意外关闭

      研究很久很久,搞不明白,不是一向都好好的嘛,难道只能网络也只需要赞美吗?代码都没动啊,以前跑得也很正常的啊,突然之间就提示基础连接已关闭,什么什么错误之类,搞了N久,都不知道为什么?蛋都痛得不行了。

以前的代码

 public static string HttpGetWebResponse(string url)
        {
            string m = string.Empty;

            Stream stream = null;
            StreamReader sr = null;

            try
            {
                WebRequest request = WebRequest.Create(url);
                WebResponse response = request.GetResponse();

                stream = response.GetResponseStream();
                sr = new StreamReader(stream, System.Text.Encoding.UTF8);
                m = sr.ReadToEnd();

                return m;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(e.Message);

                return string.Empty;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (sr != null)
                {
                    sr.Close();
                }
            }
        } 

这个有什么问题啊?有什么问题啊?有什么问题啊?不重要的事情也说三遍

不知道为什么,就是过不去。无奈之下,百般百度之下

 public static string GetPage(string posturl)
        {
            Stream instream = null;
            StreamReader sr = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(posturl);
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                request.KeepAlive = false;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                ServicePointManager.DefaultConnectionLimit = 200;

                request.ServicePoint.ConnectionLimit = 200;

                request.ContentType = "application/x-www-form-urlencoded";
                request.ProtocolVersion = HttpVersion.Version10;
                request.Timeout = 5 * 1000;
                request.Method = "GET";

                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, Encoding.UTF8);
                //返回结果网页(html)代码
                return sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("GET" + posturl + ex.Message);

                return string.Empty;
            }
            finally 
            {
                if (sr != null) 
                {
                    sr.Close();
                    sr = null;
                }

                if (instream != null) {
                    instream.Dispose();
                    instream.Close();
                    instream = null;
                }
            }
        }

发现这么写就可以了,关键代码好像就一句

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

  但是,但是,但是这样只是表示连腾讯的接口是可以,连百度的接口却不行的。我们系统是先测试的百度接口,百度搞好,连腾讯接口总是不行。着急,上火都没鸟用。

百度写如下:

public static string HttpPost(string Url, string postDataStr)
        {
            
            try
            {
                System.GC.Collect();

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                request.ProtocolVersion = HttpVersion.Version10;

                //ServicePointManager.ServerCertificateValidationCallback = new RecommendService( RemoteCertificateValidationCallback(CheckValidationResult);
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                //response.Cookies =  cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;
            }
            catch (Exception e) 
            {
                LogHelper.WriteLog("post提交失败" +Url + e.Message);
                return "";
            }
        }
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

关键代码好像就这一句

到这里似乎可以总结了,就是两个接口的安全协议不一样。

这里有两个疑问,为什么会不一样了,为什么以前就是好的了。

运行环境IIS 下 framework2.0  语言C#,网上也说如果在4.0下就不需要如此复杂。因为服务器比较老,一时半会又没有银子升级,正好解释一下什么是没钱的人命苦。

 最后了,似乎还是有点知其然不知其所以然。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值