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

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

    To expand on Bartho Bernsmann's answer, I should like to add that one can have a universal, future-proof implementation at the expense of a little reflection:

    static void AllowAllSecurityPrototols()
    {   int                  i, n;
        Array                types;
        SecurityProtocolType combined;
    
        types = Enum.GetValues( typeof( SecurityProtocolType ) ); 
        combined = ( SecurityProtocolType )types.GetValue( 0 );
    
        n = types.Length;
        for( i = 1; i < n; i += 1 )
        {   combined |= ( SecurityProtocolType )types.GetValue( i );  }
    
        ServicePointManager.SecurityProtocol = combined;
    }
    

    I invoke this method in the static constructor of the class that accesses the internet.

    0 讨论(0)
  • 2020-11-28 07:32

    None of the solutions out there worked for me. What I eventually discovered was the following combination:

    • Client system: Windows XP Pro SP3
    • Client system has .NET Framework 2 SP1, 3, 3.5 installed
    • Software targeting .NET 2 using classic web services (.asmx)
    • Server: IIS6
    • Web site "Secure Communications" set to:
      • Require Secure Channel
      • Accept client certificates

    Apparently, it was this last option that was causing the issue. I discovered this by trying to open the web service URL directly in Internet Explorer. It just hung indefinitely trying to load the page. Disabling "Accept client certificates" allowed the page to load normally. I am not sure if it was a problem with this specific system (maybe a glitched client certificate?) Since I wasn't using client certificates this option worked for me.

    0 讨论(0)
  • 2020-11-28 07:37

    I was working also on web scraping project and same issue found, below code applied and it worked nicely. If you are not aware about TLS versions then you can apply all below otherwise you can apply specific.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
    
    0 讨论(0)
  • 2020-11-28 07:38

    Before Execute query I put the statement as below and it resolved my error. Just FYI in case it will help someone.

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ctx.ExecuteQuery();

    0 讨论(0)
  • 2020-11-28 07:42

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

    This problem occurs when the server or another network device unexpectedly closes an existing Transmission Control Protocol (TCP) connection. This problem may occur when a time-out value on the server or on the network device is set too low. To resolve this problem, see resolutions A, D, E, F, and O. The problem can also occur if the server resets the connection unexpectedly, such as if an unhandled exception crashes the server process. Analyze the server logs to see if this may be the issue.

    Resolution

    To resolve this problem, make sure that you are using the most recent version of the .NET Framework.

    Add a method to the class to override the GetWebRequest method. This change lets you access the HttpWebRequest object. If you are using Microsoft Visual C#, the new method must be similar to the following.

    class MyTestService:TestService.TestService
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
            //Setting KeepAlive to false
            webRequest.KeepAlive = false;
            return webRequest;
        }
    }
    

    Excerpt from KB915599: You receive one or more error messages when you try to make an HTTP request in an application that is built on the .NET Framework 1.1 Service Pack 1.

    0 讨论(0)
  • 2020-11-28 07:42

    My Hosting server block requesting URL And code site getting the same error Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

    After a lot of time spent and apply the following step to resolve this issue

    1. Added line before the call web URL

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

    2. still issue not resolve then I upgrade .net version to 4.7.2 but I think it's optional

    3. Last change I have checked my hosting server security level which causes to TLS handshaking for this used "https://www.ssllabs.com/ssltest/index.html" site
      and also check to request URL security level then I find the difference is requested URL have to enable a weak level Cipher Suites you can see in the below image

    Now here are my hosting server supporting Cipher Suites

    here is called if you have control over requesting URL host server then you can sync this both server Cipher Suites. but in my case, it's not possible so I have applied the following script in Windows PowerShell on my hosting server for enabling required weak level Cipher Suites.

    Enable-TlsCipherSuite -Name "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"
    Enable-TlsCipherSuite -Name "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"
    Enable-TlsCipherSuite -Name "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"
    Enable-TlsCipherSuite -Name "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"
    Enable-TlsCipherSuite -Name "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"
    Enable-TlsCipherSuite -Name "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_256_GCM_SHA384"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_128_GCM_SHA256"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_256_CBC_SHA256"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_128_CBC_SHA256"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_256_CBC_SHA"
    Enable-TlsCipherSuite -Name "TLS_RSA_WITH_AES_256_CBC_SHA"

    after applying the above script my hosting server Cipher Suites level look like

    Then my issue resolved.

    Note: server security level downgrade is not a recommended option.

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