我需要升级.NET应用程序以支持在仅支持TLS 1.2的网站上调用API.根据我的阅读,如果应用程序的目标是4.6或更高,那么默认情况下它将使用TLS 1.2.
为了测试我创建了一个面向4.7的Windows窗体应用程序.不幸的是,当我没有明确设置ServicePointManager.SecurityProtocol时,它会出错.这是代码:
HttpClient _client = new HttpClient();
var msg = new StringBuilder();
// If I uncomment the next line it works, but fails even with 4.7
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://sandbox.authorize.net");
httpWebRequest.KeepAlive = false;
try
{
var httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
msg.AppendLine("The HTTP request Headers for the first request are: ");
foreach (var header in httpWebRequest.Headers)
{
msg.AppendLine(header.ToString());
}
ResponseTextBox.Text = msg.ToString();
}
catch (Exception exception)
{
ResponseTextBox.Text = exception.Message;
if (exception.InnerException != null)
{ …Run Code Online (Sandbox Code Playgroud)