我的本地和其他服务器用同一段代码都可以访问:
原代码:
cs
RestClient client = new RestClient(url);
client.Timeout = -1;
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + access_token);
request.AddHeader("Content-Type", "application/json");
CommanFunction.WriteLog2("UploadData_Error.log", "params2:" + json2);
request.AddParameter("application/json", json2, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var text = response.Content;
返回值是空,statuscode 值是0
原因:
应该是我的本机启用了TLS1.2支持,而服务器上没有启用TLS1.2的支持,所以出现同样的代码本机没问题,而服务器上没法使用。
改后代码:
cs
RestClient client = new RestClient(url);
client.Timeout = -1;
RestRequest request = new RestRequest(Method.POST);
--添加的代码
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;
--
request.AddHeader("Authorization", "Bearer " + access_token);
request.AddHeader("Content-Type", "application/json");
CommanFunction.WriteLog2("UploadData_Error.log", "params2:" + json2);
request.AddParameter("application/json", json2, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var text = response.Content;
增加的代码:
cs
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;