问题日志 : 基础连接已经关闭: 发送时发生错误。

基础连接已经关闭: 发送时发生错误。 ---> System.IO.IOException: 无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。 ---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。

WebClient 请求的时候出现 “基础连接已经关闭: 发送时发生错误” 问题,

//.net 4.0 设置: ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
//.net 4.5 设置: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

附上官方说明

https://docs.microsoft.com/zh-cn/dotnet/api/system.net.securityprotocoltype?view=netframework-4.7.2

本以为是服务器问题,没想到是设置问题

WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
wc.Headers.Add("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
wc.Headers.Add("Content-Type: application/json");
wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
result = wc.UploadString(url, "POST", data);

var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", data, ParameterType.RequestBody);

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
IRestResponse response = client.Execute(request);
result = response.Content;