如何指定要用于WebClient类的SSL协议 [英] How to specify SSL protocol to use for WebClient class

查看:224
本文介绍了如何指定要用于WebClient类的SSL协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用HTTPS POST将数据发送到服务器的应用程序。我使用System.Net.WebClient对象来执行此操作。以下是发送一些数据的函数:

I have an application that sends data to a server using an HTTPS POST. I use a System.Net.WebClient object to do this. Here is a function that sends some data:

    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://

We are passing in a URI beginning with https://

这已经很长时间了。今天,我们开始遇到以下连接错误:基础连接已关闭:发送中发生意外错误。我们与服务器所有者进行了一些故障排除,最后他们将其范围缩小到以下范围。他们更改了服务器以阻止TLS 1.0,并说我们现在需要使用TLS 1.1或1.2发送数据。

This has been working great for a long time. Today, we started getting the following connection error: "The underlying connection was closed: An unexpected error occurred on a send". We did some troubleshooting with the owner of the server, and they finally narrowed it down to the following. They made a change to their server to block TLS 1.0, and said that we now need to send our data using either TLS 1.1 or 1.2.

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

What do I need to set in my WebClient object (or elsewhere in my function) to make it use TLS 1.1 or 1.2 instead of TLS 1.0?

如果正在使用.NET Framework 4.5,

We are using .NET Framework 4.5 if that makes a difference.

推荐答案

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

From the suggested other questions, I was able to solve it by adding the following line to my code:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

此功能从客户端禁用了TLS 1.0,然后服务器接受了连接。

This disabled TLS 1.0 from the client, and then the server accepted the connection.

希望这可以帮助遇到相同问题的其他人。尽管答案与其他问题类似,但从被问到的情况来看并不清楚,所以我不认为这是重复的。

Hope this helps someone else with the same issue. Although the answer is similar to those other questions, it wasn't obvious from the questions asked that this was the case, so I don't feel that this is a duplicate.

这篇关于如何指定要用于WebClient类的SSL协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆