Clear context after HttpWebRequest

Viewed 49

We use an HttpWebRequest to send query to a webservice with NetworkCredential like this :

Byte[] bytes = Encoding.UTF8.GetBytes("...");
HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(strURL);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "application/xml";
request.Timeout = 10000;
request.KeepAlive = false;

NetworkCredential cred = new NetworkCredential
{
    UserName = "...",
    Password = "..."
};

request.Credentials = cred;
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}

HttpWebResponse resp = request.GetResponse() as HttpWebResponse;

To test it, we created a small console project with just this request and constant information (always the same). After two successful requests, the third one systematically stops and ends in timeout. But if we restart our application, we can immediately reissue two requests before the third one freezes again.

Do you have any idea of the cause? It looks like some information needs to be purged before we can continue.

-- EDIT -- I tried to start each request on a separate thread (Using Thread or Task) with the same result.

1 Answers

I change everything to use HttpClient and, now, everything works fine.

Related