HttpWebRequest timeouts

G

Glenn

Hi

I'm trying to building something to check the availability of a web page.

The code is using a HttpWebRequest, bypassing our proxy server and hitting
both HTTP/HTTPS pages. Initially I get a HTTP response code of 200. After
the second or third attempt at hitting the same page over a period of a
couple of minutes, I get a timeout. Note that I'm creating a fresh
HttpWebRequest each time I hit the web page.

I restart the console app and it works, but again, only for a couple of
attempts.

Any ideas?

Thanks

Glenn

Code below...
public override bool RunTest()

{

bool success = false;

HttpWebRequest request = WebRequest.Create( WebSiteProperties.WebSiteUrl )
as HttpWebRequest;

request.CachePolicy = new HttpRequestCachePolicy(
HttpRequestCacheLevel.BypassCache );

try

{

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

HttpStatusCode responseHttpStatusCode = response.StatusCode;

List<string> expectedHttpStatusCodes = new List<string>(

WebSiteProperties.ExpectedHttpStatusCodes.Split( ",".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries ) );

success = expectedHttpStatusCodes.Contains( (
(int)responseHttpStatusCode ).ToString() );

return success;

}

catch ( Exception exception )

{

return success;

}

}
 
M

Matt Lacey

Hi

I'm trying to building something to check the availability of a web page.

The code is using a HttpWebRequest, bypassing our proxy server and hitting
both HTTP/HTTPS pages. Initially I get a HTTP response code of 200. After
the second or third attempt at hitting the same page over a period of a
couple of minutes, I get a timeout. Note that I'm creating a fresh
HttpWebRequest each time I hit the web page.

I restart the console app and it works, but again, only for a couple of
attempts.

Any ideas?

Thanks

Glenn

Code below...
public override bool RunTest()

{

bool success = false;

HttpWebRequest request = WebRequest.Create( WebSiteProperties.WebSiteUrl )
as HttpWebRequest;

request.CachePolicy = new HttpRequestCachePolicy(
HttpRequestCacheLevel.BypassCache );

try

{

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

HttpStatusCode responseHttpStatusCode = response.StatusCode;

List<string> expectedHttpStatusCodes = new List<string>(

WebSiteProperties.ExpectedHttpStatusCodes.Split( ",".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries ) );

success = expectedHttpStatusCodes.Contains( (
(int)responseHttpStatusCode ).ToString() );

return success;

}

catch ( Exception exception )

{

return success;



}
}- Hide quoted text -

- Show quoted text -

I haven't got the code to hand but I've had something like this in the
past.
I think it comes down to not closing/disposing the HttpWebResponse
object.
 
G

Glenn

Problem solved.

I should have closed the damn response stream, damn, damn, damn, damn it!

Glenn
 
G

Glenn

Thanks Matt

Yep, you're right, although HttpWebRequest and it's response counterpart
don't implement IDisposable, you have to remember to close the streams, and
definately don't cross them ;-).

Time for the pub I think.

Glenn
 
J

Jon Skeet [C# MVP]

Glenn said:
Yep, you're right, although HttpWebRequest and it's response counterpart
don't implement IDisposable, you have to remember to close the streams, and
definately don't cross them ;-).

WebResponse implements IDisposable, but WebRequest doesn't.
 

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