WebRequest hanging, no timeout

G

Guest

I appreciate anyone's insight on this as I am new to web programming with .NET.

I have a simple method which I use to send various HTTP requests to a web
server (snippet below). On the first several invocations, the debugger will
actually stop in the catch block.

However, after a number of invocations (variant) the program just hangs on
waiting for the GetResponse(). I would expect the timeout/Webexception to
occur. In monitoring the traffic on the webserver, the requests are coming
in and the server appears to be responding appropriately.

On an additional note, if I comment out the explicit setting of the timeout
to 100 ms (from what I read in the WROX book, default is 100,000 anyway),
then the application never steps into the catch block.

bool sendWebRequest(string URIstring)
{
WebRequest webReq = WebRequest.Create(URIstring);
webReq.Timeout = 100;
try
{
WebResponse webResp = webReq.GetResponse();
return true;
}
catch (WebException e)
{
return false;
}
}
 
G

Guest

Thanks - but if I actually remove the explicit timeout, it hangs forever.

I played with the numbers and found that 1000000 msec will produce results,
but that is just wayyyy too slow for our purposes. I think I'm going to need
to bypass .NET's web widgets and go the low-level socket route.

Which doesn't make sense, because the response in IE is super fast...?
 
J

Joerg Jooss

Thus wrote (e-mail address removed).(D0N0tSp@m),
I appreciate anyone's insight on this as I am new to web programming
with .NET.

I have a simple method which I use to send various HTTP requests to a
web server (snippet below). On the first several invocations, the
debugger will actually stop in the catch block.

However, after a number of invocations (variant) the program just
hangs on waiting for the GetResponse(). I would expect the
timeout/Webexception to occur. In monitoring the traffic on the
webserver, the requests are coming in and the server appears to be
responding appropriately.

On an additional note, if I comment out the explicit setting of the
timeout to 100 ms (from what I read in the WROX book, default is
100,000 anyway), then the application never steps into the catch
block.

bool sendWebRequest(string URIstring)
{
WebRequest webReq = WebRequest.Create(URIstring);
webReq.Timeout = 100;
try
{
WebResponse webResp = webReq.GetResponse();
return true;
}
catch (WebException e)
{
return false;
}
}

HttpWebResponse is an IDisposable. You have to close it explictly, otherwise
it blocks an underlying TCP connection -- and by default you only get two
persistent connections per server. Use a using block to safely dispose the
response:

using(WebResponse response = request.GetResponse()
{
// Process response...
}

Cheers,
 

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