• XP环境下的网络证书问题


    项目过程中,由于是收银系统需要从服务器获取支付二维码,会产生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+" "+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; } }
  • 相关阅读:
    C#使用Json
    JQuery AJAX介绍
    封装一个自己的 Ajax小框架
    AJAX跨域实现
    AJAX 一些常用方法
    完整的 AJAX 写法(支持多浏览器)
    AJAX 简单上手
    利用反射生成SQL语句
    访问Access数据库(有多个数据库时 体现多态)
    DataTable操作(建表,建行,建列,添加数据)
  • 原文地址:https://www.cnblogs.com/bryantzx/p/7845345.html
Copyright © 2020-2023  润新知