如何指定用于 WebClient 的 SSL 协议 class
How to specify SSL protocol to use for WebClient class
我有一个使用 HTTPS POST 将数据发送到服务器的应用程序。我使用 System.Net.WebClient 对象来执行此操作。这是一个发送一些数据的函数:
private byte[] PostNameValuePairs(string uri, NameValueCollection pairs)
{
byte[] response;
String responsestring = "";
using (WebClient client = new WebClient())
{
client.Headers = GetAuthenticationHeader();
string DataSent = GetNameValueCollectionValuesString(pairs);
try
{
response = client.UploadValues(uri, pairs);
responsestring = Encoding.ASCII.GetString(response);
}
catch (Exception e)
{
responsestring = "CONNECTION ERROR: " + e.Message;
return Encoding.ASCII.GetBytes(responsestring);
}
finally
{
_communicationLogger.LogCommunication(uri, client.Headers.ToString(), DataSent, responsestring);
}
}
return response;
}
我们正在传递以 https://
开头的 URI
这已经运行了很长时间了。今天,我们开始收到以下连接错误:"The underlying connection was closed: An unexpected error occurred on a send"。我们与服务器所有者进行了一些故障排除,他们最终将范围缩小到了以下几点。他们更改了服务器以阻止 TLS 1.0,并表示我们现在需要使用 TLS 1.1 或 1.2 发送数据。
我需要在我的 WebClient 对象(或我的函数中的其他地方)中设置什么才能使其使用 TLS 1.1 或 1.2 而不是 TLS 1.0?
我们使用的是 .NET Framework 4.5,如果这有所不同的话。
根据建议的其他问题,我可以通过在我的代码中添加以下行来解决它:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
这从客户端禁用了 TLS 1.0,然后服务器接受了连接。
希望这对遇到同样问题的其他人有所帮助。虽然答案与其他问题相似,但从所问的问题中看不出是这种情况,所以我不认为这是重复的。
我在c#中找到了一个更严格的开启和关闭TLS版本的版本。
这可以与 .Net 4.5 及更高版本一起使用。
// Remove insecure protocols (SSL3, TLS 1.0, TLS 1.1)
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls11;
// Add TLS 1.2
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
这确保即使服务器能够使用 1.0 或 1.1,我们也可以明确排除这种情况的发生。
我有一个使用 HTTPS POST 将数据发送到服务器的应用程序。我使用 System.Net.WebClient 对象来执行此操作。这是一个发送一些数据的函数:
private byte[] PostNameValuePairs(string uri, NameValueCollection pairs)
{
byte[] response;
String responsestring = "";
using (WebClient client = new WebClient())
{
client.Headers = GetAuthenticationHeader();
string DataSent = GetNameValueCollectionValuesString(pairs);
try
{
response = client.UploadValues(uri, pairs);
responsestring = Encoding.ASCII.GetString(response);
}
catch (Exception e)
{
responsestring = "CONNECTION ERROR: " + e.Message;
return Encoding.ASCII.GetBytes(responsestring);
}
finally
{
_communicationLogger.LogCommunication(uri, client.Headers.ToString(), DataSent, responsestring);
}
}
return response;
}
我们正在传递以 https://
开头的 URI这已经运行了很长时间了。今天,我们开始收到以下连接错误:"The underlying connection was closed: An unexpected error occurred on a send"。我们与服务器所有者进行了一些故障排除,他们最终将范围缩小到了以下几点。他们更改了服务器以阻止 TLS 1.0,并表示我们现在需要使用 TLS 1.1 或 1.2 发送数据。
我需要在我的 WebClient 对象(或我的函数中的其他地方)中设置什么才能使其使用 TLS 1.1 或 1.2 而不是 TLS 1.0?
我们使用的是 .NET Framework 4.5,如果这有所不同的话。
根据建议的其他问题,我可以通过在我的代码中添加以下行来解决它:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
这从客户端禁用了 TLS 1.0,然后服务器接受了连接。
希望这对遇到同样问题的其他人有所帮助。虽然答案与其他问题相似,但从所问的问题中看不出是这种情况,所以我不认为这是重复的。
我在c#中找到了一个更严格的开启和关闭TLS版本的版本。
这可以与 .Net 4.5 及更高版本一起使用。
// Remove insecure protocols (SSL3, TLS 1.0, TLS 1.1)
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls11;
// Add TLS 1.2
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
这确保即使服务器能够使用 1.0 或 1.1,我们也可以明确排除这种情况的发生。