views:

211

answers:

0

My application makes multiple HttpWebRequests to a REST webservice sometimes as many as 50 for a single page. I have started intermittently getting the following WebException, which occurs on random endpoints

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

The application is on IIS6, Windows Server 2003, .NET Framework 2.0

All of the requests go through a single method "Request"...

public class ResourceRequest
{
    private HttpWebRequest m_Request;
    private HttpWebResponse m_Response;
    private int m_StatusCode;
    private XPathNavigator m_Navigator;

    public string GetXml()
    {
        Request("/foo","GET");

        if (m_StatusCode == 200)
        {
            XPathDocument xDoc = new XPathDocument(m_Response.GetResponseStream());

            m_Response.Close();
            m_Response = null;

            m_Navigator = xDoc.CreateNavigator();

            ...
        }

    }

    private void Request(string uri, string method)
    {
        m_Request = (HttpWebRequest)WebRequest.Create(uri);        
        m_Request.Accept = "text/xml,application/xml";
        m_Request.Method = method;
        m_Request.KeepAlive = false;

        if (method == "POST")
        {
            m_Request.ContentLength = 0;
        }

        try
        {
            m_Response = (HttpWebResponse)m_Request.GetResponse();
        }
        catch (WebException webEx)
        {
            if (webEx.Status = WebExceptionStatus.ProtocolError)
            {
                m_StatusCode = (int)((HttpWebResponse)webEx.Response).StatusCode;
            }
        }

        if (m_Response != null)
        {
            m_StatusCode = (int)m_Response.StatusCode;
        }
    }
}

I'm at a bit of a loss as to the cause of this, it would be greatly appreciated if anyone could point me in the right direction

Thanks!