The remote host closed the connection. The error code is 0x800704CD

前端 未结 6 1121
说谎
说谎 2020-12-03 00:33

I receive error emails from my website whenever an exception occurs. I am getting this error:

The remote host closed the connection. The error code is

相关标签:
6条回答
  • 2020-12-03 00:44

    I was getting this on an asp.net 2.0 iis7 Windows2008 site. Same code on iis6 worked fine. It was causing an issue for me because it was messing up the login process. User would login and get a 302 to default.asxp, which would get through page_load, but not as far as pre-render before iis7 would send a 302 back to login.aspx without the auth cookie. I started playing with app pool settings, and for some reason 'enable 32 bit applications' seems to have fixed it. No idea why, since this site isn't doing anything special that should require any 32 bit drivers. We have some sites that still use Access that require 32bit, but not our straight SQL sites like this one.

    0 讨论(0)
  • 2020-12-03 00:50

    One can reproduce the error with the code below:

    public ActionResult ClosingTheConnectionAction(){
       try
       {
          //we need to set buffer to false to
          //make sure data is written in chunks
          Response.Buffer = false;  
          var someText = "Some text here to make things happen ;-)";
          var content = GetBytes( someText );
    
          for(var i=0; i < 100; i++)
          {
             Response.OutputStream.Write(content, 0, content.Length);
          }
    
          return View();
       }
       catch(HttpException hex)
       {
          if (hex.Message.StartsWith("The remote host closed the connection. The error code is 0x800704CD."))
                {
                    //react on remote host closed the connection exception.
                    var msg = hex.Message;
                }  
       }
       catch(Exception somethingElseHappened)
       {
          //handle it with some other code
       }
    
       return View();
    } 
    

    Now run the website in debug mode. Put a breakpoint in the loop that writes to the output stream. Go to that action method and after the first iteration passed close the tab of the browser. Hit F10 to continue the loop. After it hit the next iteration you will see the exception. Enjoy your exception :-)

    0 讨论(0)
  • 2020-12-03 00:51

    As m.edmondson mentioned, "The remote host closed the connection." occurs when a user or browser cancels something, or the network connection drops etc. It doesn't necessarily have to be a file download however, just any request for any resource that results in a response to the client. Basically the error means that the response could not be sent because the server can no longer talk to the client(browser).

    There are a number of steps that you can take in order to stop it happening. If you are manually sending something in the response with a Response.Write, Response.Flush, returning data from a web servivce/page method or something similar, then you should consider checking Response.IsClientConnected before sending the response. Also, if the response is likely to take a long time or a lot of server-side processing is required, you should check this periodically until the response.end if called. See the following for details on this property:

    http://msdn.microsoft.com/en-us/library/system.web.httpresponse.isclientconnected.aspx

    Alternatively, which I believe is most likely in your case, the error is being caused by something inside the framework. The following link may by of use:

    http://blog.whitesites.com/fixing-The-remote-host-closed-the-connection-The-error-code-is-0x80070057__633882307305519259_blog.htm

    The following stack-overflow post might also be of interest:

    "The remote host closed the connection" in Response.OutputStream.Write

    0 讨论(0)
  • I got this error when I dynamically read data from a WebRequest and never closed the Response.

        protected System.IO.Stream GetStream(string url)
        {
            try
            {
                System.IO.Stream stream = null;
                var request = System.Net.WebRequest.Create(url);
                var response = request.GetResponse();
    
                if (response != null) {
                    stream = response.GetResponseStream();
    
                    // I never closed the response thus resulting in the error
                    response.Close(); 
                }
                response = null;
                request = null;
    
                return stream;
            }
            catch (Exception) { }
            return null;
        }
    
    0 讨论(0)
  • 2020-12-03 01:00

    I too got this same error on my image handler that I wrote. I got it like 30 times a day on site with heavy traffic, managed to reproduce it also. You get this when a user cancels the request (closes the page or his internet connection is interrupted for example), in my case in the following row:

    myContext.Response.OutputStream.Write(buffer, 0, bytesRead);
    

    I can’t think of any way to prevent it but maybe you can properly handle this. Ex:

            try
            {
                …
                myContext.Response.OutputStream.Write(buffer, 0, bytesRead);
                …
            }catch (HttpException ex)
            {
                if (ex.Message.StartsWith("The remote host closed the connection."))
                    ;//do nothing
                else
                    //handle other errors
            }            
            catch (Exception e)
            {
                //handle other errors
            }
            finally
            {//close streams etc..
            }
    
    0 讨论(0)
  • 2020-12-03 01:02

    I get this one all the time. It means that the user started to download a file, and then it either failed, or they cancelled it.

    To reproduce the exception try do this yourself - however I'm unaware of any ways to prevent it (except for handling this specific exception only).

    You need to decide what the best way forward is depending on your app.

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