相关疑难解决方法(0)

没有明确的ServicePointManager.SecurityProtocol调用,在.NET 4.7中没有协商TLS 1.2

我需要升级.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)

.net c# httpwebrequest winforms tls1.2

31
推荐指数
5
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

httpwebrequest ×1

tls1.2 ×1

winforms ×1