TheLearner
TheLearner

Reputation: 19507

The underlying connection was closed error

Is anyone familiar with this issue, I seem to get it every now and then in my web service client:

The underlying connection was closed: An unexpected error occurred on a receive. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()

This is the code that I use for requests and responses to the web service:

 public string GetWebResponse(string data, IOffer offer)
    {
        _loggingManager.LogProcess(offer, true, "Request", "Request", data, offer.OperatorCode, true);

        DateTime start = DateTime.Now;
        StringBuilder returnedXml = new StringBuilder();
        string outputstring = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();
        StreamReader r = null;

        /*try
        {*/
            if (!string.IsNullOrEmpty(data))
                outputstring = Regex.Replace(data, "\\s+", " ");

            outputstring = outputstring.Replace("utf-16", "utf-8");
            string PostData = "xml=" + outputstring;

            byte[] Bytedata = encoding.GetBytes(PostData);

            //fixme
            objWebHTTPReq = (HttpWebRequest)WebRequest.Create(GetEndpointAddress(offer));

            objWebHTTPReq.ContentType = "application/x-www-form-urlencoded";
            objWebHTTPReq.Accept = "text/html";
            objWebHTTPReq.ContentLength = Bytedata.Length;
            objWebHTTPReq.Method = "POST";

            objWebHTTPReq.KeepAlive = false;

            string httpOutput = objWebHTTPReq.Headers.ToString();

            objStream = objWebHTTPReq.GetRequestStream();
            objStream.Write(Bytedata, 0, Bytedata.Length);
            objStream.Flush();
            objStream.Close();

            WebResponse resp = objWebHTTPReq.GetResponse();
            r = new StreamReader(resp.GetResponseStream());

            returnedXml.Append(r.ReadToEnd().Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Replace("<?xml version=\"1.0\" encoding=\"utf-8\" ?>", "").ToString());

            _loggingManager.LogProcess(offer, true, "Response", "Response", returnedXml.ToString(), offer.OperatorCode, true);

            return returnedXml.ToString();

        /*}
        catch (Exception ex)
        {
            return null;
            // fixme
            //return this.CreateErrorResponse("Handled exception:\n" + ex.ToString());
        }
        finally
        {
            if (!string.IsNullOrEmpty(outputstring))
                outputstring.Remove(0);
            if (returnedXml != null)
                returnedXml.Remove(0, returnedXml.Length);
            if (r != null)
                r.Dispose();
        }*/
    }         

Upvotes: 2

Views: 4628

Answers (2)

Rune FS
Rune FS

Reputation: 21742

The message is at times rather misleading. You can get the same message when there's no route to the remote host.

When you get the error try telnet on the same port. If that works it seems to be a timeout if you cannot telnet then it's a connection issue (no route to host)

Upvotes: 2

Kamran Khan
Kamran Khan

Reputation: 9986

Try setting the .Timeout for your web service call.

yourWebService.Timeout = -1;// never timeout.

Also you can try setting executionTimeout in your web.config:

<configuration>
  <system.web>
  <httpRuntime maxRequestLength="4000"
    executionTimeout="45"
  </system.web>
</configuration>

Upvotes: 2

Related Questions