用C#封装的网络模块接口,用到了webSocket4Net和RestSharp,在debug的时候一开始都出现了类似以下错误:
请求被中止: 未能创建 SSL/TLS 安全通道。
System.Net.WebException: 请求被中止: 未能创建 SSL/TLS 安全通道。
在 System.Net.HttpWebRequest.GetResponse()
在 RestSharp.Http.GetRawResponse(HttpWebRequest request)
在 RestSharp.Http.GetResponse(HttpWebRequest request)
WebSocket4Net解决方法:
在实例化WebSocket的时候,指定ssl级别,Tls12;
this.websocket = new WebSocket(API_ADDR, "", null, null, "", "", WebSocketVersion.None, null, System.Security.Authentication.SslProtocols.Tls12, 0);
RestSharp解决方法:
在实例化RestSharp的时候,同样指定ssl级别,Tls12;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
this.client = new RestClient(API_URL);
this.client.AddDefaultHeader("Content-Type", "application/json");
如上面只需要加一句就可以解决这个问题了。当然,在RestSharp中,还可以同时指定成这样,:
System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
this.client = new RestClient(API_URL);
this.client.AddDefaultHeader("Content-Type", "application/json");
建议还是第一种方式就足够了,因为更安全