Timeout with HttpWebRequest

D

donler

I call this code multiple times. After some period of time (I don't know how
long, sometimes after a short period and then it will recover),
(HttpWebResponse)webRequest.GetResponse() starts to timeout. At some point
it will continue to timeout and never recover until I stop the app and start
it backup. Here is the code.

Thx for any help



public string SendRequestTo( WebHeaderCollection whc, byte[] requestBytes,
string method,
string accept, bool autoRedirect, string contentType, bool keepAlive,
Uri destination, NetworkCredential credential )
{
Debug.WriteLine( "Sending request to url:" + destination.AbsoluteUri );

// Build the request.
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);

//Add headers if there are any
if ( whc != null && whc.Count > 0 )
{
webRequest.Headers = new WebHeaderCollection();
webRequest.Headers = whc;
}

webRequest.Method = method;
webRequest.Accept = accept;
webRequest.AllowAutoRedirect = autoRedirect;
webRequest.UserAgent = "Outlook-Express/6.0 (MSIE 6.0; Windows NT 5.1;
Q312461; " +
".NET CLR 1.0.3705; .NET CLR 1.1.4322; TmstmpExt)";
webRequest.KeepAlive = keepAlive;

if ( requestBytes != null )
webRequest.ContentLength = requestBytes.Length;
else
webRequest.ContentLength = 0;

webRequest.ContentType = contentType;

// Set the credentials
webRequest.Credentials = credential;

// Add cookies for this request
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.Add(_ccContainer.GetCookies(destination));

try
{
// Write the request
Stream reqStream = webRequest.GetRequestStream();

//Only write the bytes if the byte buffer isn't null
if ( requestBytes != null )
reqStream.Write( requestBytes, 0, requestBytes.Length );

reqStream.Close();

//******************** Problem Child **********************//
// Get a response
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

//******************** Problem Child **********************//

if (webRequest.HaveResponse)
{
// Handle returned cookies
foreach(Cookie retCookie in webResponse.Cookies)
{
bool cookieFound = false;

foreach( Cookie oldCookie in _ccContainer.GetCookies( destination ) )
{
if ( retCookie.Name.Equals( oldCookie.Name ) )
{
oldCookie.Value = retCookie.Value;
cookieFound = true;
}
}

if ( !cookieFound )
_ccContainer.Add( retCookie );
}

// Handle redirection headers
if ((webResponse.StatusCode == HttpStatusCode.Found) ||
(webResponse.StatusCode == HttpStatusCode.Redirect) ||
(webResponse.StatusCode == HttpStatusCode.Moved) ||
(webResponse.StatusCode == HttpStatusCode.MovedPermanently))
{
WebHeaderCollection headers = webResponse.Headers;
return this.SendRequestTo( whc, requestBytes, method, accept,
autoRedirect, contentType, keepAlive,
new Uri( headers["location"] ), credential );
}

// Read response
StreamReader stream = new StreamReader(webResponse.GetResponseStream());
string responseString = stream.ReadToEnd();
stream.Close();
return responseString;
}

// No response
Debug.WriteLine("No response received from host.");
throw new ApplicationException("No response received from host.");
}
catch(WebException e)
{
// Error occured
Debug.WriteLine("WebException: " + e.ToString());
throw new ApplicationException("WebException: ", e);
}
catch(Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
throw new ApplicationException("Exception: ", ex);
}
}
 
J

Joerg Jooss

donler said:
I call this code multiple times. After some period of time (I don't
know how long, sometimes after a short period and then it will
recover), (HttpWebResponse)webRequest.GetResponse() starts to
timeout. At some point it will continue to timeout and never recover
until I stop the app and start it backup. Here is the code.

I guess you're running into the maximum number of connections per
ServicePoint. You can increase the value by setting
ServicePointManager.DefaultConnectionLimit to a higher number (default
is 2 for persistent and 4 for nin-persistent connections).

Cheers,
 
D

donler

I changed the DefaultConnectionLimit to 10 and this doesn't help.

thx for your help
 
F

Feroze [MSFT]

Make sure you close the respose object, or the stream you get back from
resp.GetResponseStream().

--
Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm




donler said:
I changed the DefaultConnectionLimit to 10 and this doesn't help.

thx for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top