如何指定用于WebClient类的SSL协议

Joe*_*jr2 35 .net c# ssl webclient

我有一个应用程序使用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;
    }
Run Code Online (Sandbox Code Playgroud)

我们传入一个以https://开头的URI

这已经很长时间了.今天,我们开始收到以下连接错误:"基础连接已关闭:发送时发生意外错误".我们对服务器的所有者进行了一些故障排除,他们最终将其缩小到以下内容.他们对服务器进行了更改以阻止TLS 1.0,并表示我们现在需要使用TLS 1.1或1.2发送数据.

我需要在我的WebClient对象(或我的函数中的其他位置)中设置什么才能使用TLS 1.1或1.2而不是TLS 1.0?

我们正在使用.NET Framework 4.5,如果这有所作为.

Joe*_*jr2 67

从建议的其他问题,我能够通过在我的代码中添加以下行来解决它:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

这从客户端禁用TLS 1.0,然后服务器接受连接.

希望这可以帮助其他人解决同样的问题.虽然答案与其他问题的答案相似,但从提出的问题来看并不是很明显,所以我不认为这是重复的.