标签:result 连接 encoding agent string lis ble word log
项目过程中,由于是收银系统需要从服务器获取支付二维码,会产生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; } }
标签:result 连接 encoding agent string lis ble word log
原文地址:http://www.cnblogs.com/bryantzx/p/7845345.html