如果服务器仅支持更高的TLS版本(例如仅TLS
1.2),则除非您的客户端PC默认配置为使用更高的TLS版本,否则服务器仍将失败。要解决此问题,请在代码中添加以下内容。
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
修改您的示例代码,它将是
HttpClient httpClient = new HttpClient();//specify to use TLS 1.2 as default connectionSystem.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;httpClient.baseAddress = new Uri("https://foobar.com/");httpClient.DefaultRequestHeaders.Accept.Clear();httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));var task = httpClient.PostAsXmlAsync<DeviceRequest>("api/SaveData", request);


