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

前端 未结 8 634
一个人的身影
一个人的身影 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

提交回复
热议问题