[轉載]使用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();  
相關文章
相關標籤/搜索