locked
"An existing connection was forcibly closed by the remote host." RRS feed

  • Question

  • Below is my overridden GetWebRequest. If I remove lines marked with '1' and '2' below, I dont get any error. But since I want to compress data sent over the network, I uncomment these and I get error

    "An existing connection was forcibly closed by the remote host."

    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest req = base.GetWebRequest(uri);
    
        byte[] credentialBuffer = new UTF8Encoding().GetBytes(Settings.Get("usr") + ":" + Settings.Get("pwd"));
        req.Headers.Add("Accept-Encoding", "gzip, deflate"); //1
        req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
    
        if (req is HttpWebRequest)
        {
            (req as HttpWebRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;  //2
        }
    
        return req;
    }

    I noticed that I get this only if I am sending lots of data to client. Then I tried putting break-point in my GetWebResponse() and than waiting at the break-point for couple of minutes once response comes back and I was getting same error regardless of data size being sent over network. So, this error seems to have something with timing rather than size of data.

    Message: 
    An existing connection was forcibly closed by the remote host.
    InnerException:
    N/A
    Stack Trace:
       at System.Net.Sockets.Socket.ReceiveNoCheck(Byte[] buffer, Int32 index, Int32 request, SocketFlags socketFlags)
       at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
       at System.Net.Connection.Read(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.HttpWebRequest.ConnectionClient.Read(Byte[] data, Int32 offset, Int32 length)
       at System.Net.HttpReadStream.NetworkRead(Byte[] data, Int32 offset, Int32 length)
       at System.Net.ContentLengthReadStream.doRead(Byte[] data, Int32 offset, Int32 length)
       at System.Net.HttpReadStream.Read(Byte[] buffer, Int32 offset, Int32 length)
       at System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)
       at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
       at System.Xml.XmlTextReaderImpl.ReadData()
       at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
       at System.Xml.XmlTextReaderImpl.ParseAttributes()
       at System.Xml.XmlTextReaderImpl.ParseElement()
       at System.Xml.XmlTextReaderImpl.ParseElementContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at System.Xml.XmlTextReader.Read()
       at System.Xml.XmlReader.ReadEndElement()
       at System.Xml.Serialization.XmlSerializationReader.DeserializePrimitiveElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
       at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
       at System.Xml.Serialization.XmlSerializationReader.DeserializeElementMember(MemberValueCollection members, Object fixupTarget, Hashtable internalState, Boolean& firstElement)
       at System.Xml.Serialization.XmlSerializationReader.deserializeMembers(MemberValueCollection members, Object fixupTarget)
       at System.Xml.Serialization.XmlSerializationReader.DeserializeComplexElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
       at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
       at System.Xml.Serialization.XmlSerializationReader.DeserializeElementMember(MemberValueCollection members, Object fixupTarget, Hashtable internalState, Boolean& firstElement)
       at System.Xml.Serialization.XmlSerializationReader.deserializeMembers(MemberValueCollection members, Object fixupTarget)
       at System.Web.Services.Protocols.SoapMessageParser.execute()
       at System.Web.Services.Protocols.SoapMessageSerializer.Deserialize(XmlReader reader, SoapHttpClientProtocol client, LogicalSoapMethod soapMethod, Boolean soap12)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.doInvoke(String methodName, Object[] parameters, WebClientAsyncResult asyncResult)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at MyApp.MyWS.IMyService.Login(Int32 SessionID, String SecurityHash)
       at MyApp.MainForm.loginButton_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
       at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
       at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
       at System.Windows.Forms.Application.Run(Form fm)
       at MyApp.MainForm.Main()

    VS2008 Output tab will also show several of these

    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

    Much appreciated,

    -- d.

    Sunday, September 6, 2015 4:23 PM

Answers

  • Hi _db_,

    According to this case, may be you can try  setting Agent property.

    Like below:

    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24

    For more information, please refer the following links:

    1.HttpWebRequest.GetResponse Operation has timed out

    Best Regards,

    Grady

    • Marked as answer by _db_ Monday, September 7, 2015 11:46 PM
    • Unmarked as answer by _db_ Tuesday, September 8, 2015 12:51 AM
    • Proposed as answer by Grady_Dong Monday, September 14, 2015 7:37 AM
    • Marked as answer by Grady_Dong Tuesday, September 15, 2015 7:25 AM
    Monday, September 7, 2015 7:50 AM

All replies

  • Well, it's either taking too much time and it is timing out, or you are sending too much data, and it's blowing up on that. With either one, you should be able to set the settings to use other than the default, which you'll have to lookup with Bing or Google and find out how to do it.
    Sunday, September 6, 2015 5:47 PM
  • Hi _db_,

    According to this case, may be you can try  setting Agent property.

    Like below:

    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24

    For more information, please refer the following links:

    1.HttpWebRequest.GetResponse Operation has timed out

    Best Regards,

    Grady

    • Marked as answer by _db_ Monday, September 7, 2015 11:46 PM
    • Unmarked as answer by _db_ Tuesday, September 8, 2015 12:51 AM
    • Proposed as answer by Grady_Dong Monday, September 14, 2015 7:37 AM
    • Marked as answer by Grady_Dong Tuesday, September 15, 2015 7:25 AM
    Monday, September 7, 2015 7:50 AM
  • Hi _db_,

    According to this case, may be you can try  setting Agent property.

    Like below:

    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24

    For more information, please refer the following links:

    1.HttpWebRequest.GetResponse Operation has timed out

    Best Regards,

    Grady

    Thank you for the answer.  Interesting, I added exactly what you suggested and it started working but then I commented that same line and reissued request again and it still works now???!!!???


    -- d.


    • Edited by _db_ Monday, September 7, 2015 1:42 PM
    Monday, September 7, 2015 1:42 PM
  • Hi _db_,

    According to this case, may be you can try  setting Agent property.

    Like below:

    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24

    For more information, please refer the following links:

    1.HttpWebRequest.GetResponse Operation has timed out

    Best Regards,

    Grady


    Unfortunatelly, while this has removed the "conection was forcibly closed by remote host", I am now getting "The operation has timed out" error?

    -- d.

    Tuesday, September 8, 2015 12:36 AM
  • Hi _db_,

    According to this issue >>The operation has timed out.  I have replied this issue on

    another thread which is posted of you.

    Best Regards,

    Grady

    Tuesday, September 8, 2015 5:43 AM