[转载]使用HttpWebRequest进行请求时发生错误:基础链接已关闭,发送时发生错误处理

转载,原文来自 http://blog.csdn.net/hawksoft/article/details/21776009微信

 

最近调试原来的微信模拟登录时发生了“基础链接已关闭,发送时发生错误”的错误提示,原来都是好好的,只是好久没用了。cookie

出错代码以下:spa

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN");///cgi-bin/loginpage?t=wxm2-login&lang=zh_CN  
            req.CookieContainer = cookie;  
            req.Method = "GET";  
            req.ProtocolVersion = HttpVersion.Version10;  
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();  
            StreamReader rd = new StreamReader(res.GetResponseStream());  
            string theContent = rd.ReadToEnd();  

  

通过一番Google和试验,答案既如此简单,增长以下一句:.net

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
经过代码以下:调试

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN");///cgi-bin/loginpage?t=wxm2-login&lang=zh_CN           code

 

req.CookieContainer = cookie;  
            req.Method = "GET";  
            req.ProtocolVersion = HttpVersion.Version10;  
           ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();  
            StreamReader rd = new StreamReader(res.GetResponseStream());  
            string theContent = rd.ReadToEnd();  
相关文章
相关标签/搜索