XP環境下的網絡證書問題
阿新 • • 發佈:2017-11-16
ida msi useragent sage msg 響應 requests system public
項目過程中,由於是收銀系統需要從服務器獲取支付二維碼,會產生SSL連接的問題,在win7、win10上都沒有問題,放到WIN XP上出現了The underlying connection was closed:could not establish trust,
經過查閱相關資料發現是證書問題,采用了最簡單的快捷的禁止證書驗證,代碼如下
//將客戶端訪問的證書修改為無證書 ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
代碼放置的位置為進行HTTPwebRequest請求時,完全代碼如下
/// <summary> /// 建立連接 /// </summary> /// <param name="sendmenssage">發送字符串 /// <param name="message"></param> /// <returns></returns> public bool HttpConn(string sendmenssage, out string message) {try { //將客戶端訪問的證書修改為無證書 ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
request = (HttpWebRequest)WebRequest.Create(Common.Const.QueryUrl);//原因是這 每次都是一個新的request 上次abort不影響 request.Method = "POST"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)"; // request.ContentType = "application/octet-stream";//get request.ContentType = "application/x-www-form-urlencoded;";//post // request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; request.Accept = "*/*"; request.Timeout = 20 * 1000; request.AllowAutoRedirect = true; StringBuilder data = new StringBuilder(); data.Append(sendmenssage); // MyMsgBox.Show("send:" + sendmenssage+"\n "+this.GetHashCode()); Byte[] bytes = System.Text.Encoding.Default.GetBytes(data.ToString()); request.ContentLength = bytes.Length; //AsyncCallback ac1 = null; //ac1 = new AsyncCallback((IAsyncResult r) => { Stream writer = request.EndGetRequestStream(r); writer.Write(bytes, 0, bytes.Length); writer.Close(); }); //request.BeginGetRequestStream(ac1, null); using (Stream writer = request.GetRequestStream()) {//同樣的請求會卡在這 writer.Write(bytes, 0, bytes.Length); writer.Close(); } HttpWebResponse response; // 獲得響應流 using (response = (HttpWebResponse)request.GetResponse()) { Stream s; s = response.GetResponseStream(); StreamReader reader = new StreamReader(s); StringBuilder sb = new StringBuilder(); string tmp = reader.ReadLine(); while (tmp != null) { sb.Append(tmp); tmp = reader.ReadLine(); } reader.Close(); message = sb.ToString(); } request.Abort(); return true; } catch (Exception e) { Log.WriteLog("服務器連接異常:" + e.Message); message = ""; return false; } }
XP環境下的網絡證書問題