..The underlying connection was closed: An unexpected error occurred on a receive

前端 未结 8 554
一个人的身影
一个人的身影 2020-11-28 06:49

I have the following code:

private Uri currentUri;

private void Form1_Load(object sender, EventArgs e)
{
    currentUri = new Uri(@\"http://www.stackoverflo         


        
相关标签:
8条回答
  • 2020-11-28 07:44

    Setting the HttpWebRequest.KeepAlive to false didn't work for me.

    Since I was accessing a HTTPS page I had to set the Service Point Security Protocol to Tls12.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    Notice that there are other SecurityProtocolTypes: SecurityProtocolType.Ssl3, SecurityProtocolType.Tls, SecurityProtocolType.Tls11

    So if the Tls12 doesn't work for you, try the three remaining options.

    Also notice that you can set multiple protocols. This is preferable on most cases.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    

    Edit: Since this is a choice of security standards it's obviously best to go with the latest (TLS 1.2 as of writing this), and not just doing what works. In fact, SSL3 has been officially prohibited from use since 2015 and TLS 1.0 and TLS 1.1 will likely be prohibited soon as well. source: @aske-b

    0 讨论(0)
  • 2020-11-28 07:46
    • .NET 4.6 and above. You don’t need to do any additional work to support TLS 1.2, it’s supported by default.
    • .NET 4.5. TLS 1.2 is supported, but it’s not a default protocol. You need to opt-in to use it. The following code will make TLS 1.2 default, make sure to execute it before making a connection to secured resource:
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    • .NET 4.0. TLS 1.2 is not supported, but if you have .NET 4.5 (or above) installed on the system then you still can opt in for TLS 1.2 even if your application framework doesn’t support it. The only problem is that SecurityProtocolType in .NET 4.0 doesn’t have an entry for TLS1.2, so we’d have to use a numerical representation of this enum value:
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    • .NET 3.5 or below. TLS 1.2 is not supported. Upgrade your application to more recent version of the framework.

    0 讨论(0)
提交回复
热议问题