(微信API接口開發) 使用HttpWebRequest進行請求時發生錯誤:基礎連接已關閉,發送時發生錯誤處理


最近調試原來的微信模擬登陸時發生了“基礎連接已關閉,發送時發生錯誤”的錯誤提示,原來都是好好的,只是很久沒用了。

出錯代碼如下:

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和試驗,答案既如此簡單,增加如下一句:

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

1 req.CookieContainer = cookie;  
2             req.Method = "GET";  
3             req.ProtocolVersion = HttpVersion.Version10;  
4            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  
5             HttpWebResponse res = (HttpWebResponse)req.GetResponse();  
6             StreamReader rd = new StreamReader(res.GetResponseStream());  
7             string theContent = rd.ReadToEnd();  

 

微信支付退款時,報錯 "基礎連接已經關閉: 連接被意外關閉"

使用一下方法調用證書就能解決錯誤(實測環境Windows2003)

X509Certificate2 certificate = new X509Certificate2(PATH_TO_CERTIFICATE, PASSWORD, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);

 

轉自:http://blog.csdn.net/hawksoft/article/details/21776009

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM