handling httpwebrequest timeouts

wasps

Member
Joined
Feb 18, 2010
Messages
7
Programming Experience
Beginner
Hi,

I have a windows form app that initially reads a single line of data from a text file on a remote web server

I have been using webclient component to do this and it appears to work very well.


However, I encountered an issue today when running it on a site where I didn't have any internet access (actually didn't have authentication against the proxy server)
Basically, the app couldn't access the file on the web server.

The same problem will obviously occur if my remote web server is down or temporarily unavailable.


As such, I would like to build in a routine to check if the remote web server exists first.
If the web server didn't exist, then I'd like a separate routine to run.

I've been trying to do this using a timeout. I.e. if the remote web server doesn't respond within 10 seconds, then fire my other routine.


I've therefore changed my webclient component to httpwebrequest.
I have put a timeout in as well and that works fine.

However, I can't for the life of see how to manage this connection.
I was expecting to find a httpwebrequest.status to tell me whether or not I was connected to the web server.



How should I go about determining whether or not i've made a connection to a webserver, or whether the timeout has been reached?


Thank you
 
I thought the documentation was rather clear, didn't you read it or didn't you understand it? HttpWebRequest.Timeout Property (System.Net)
Timeout is the number of milliseconds that a subsequent synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. The Timeout applies to the entire request and response, not individually to the GetRequestStream and GetResponse method calls. If the resource is not returned within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus.Timeout.
To catch an exception use the Try-Catch block. Try...Catch...Finally Statement (Visual Basic)

My.Computer.Network Object - this also allow you to check connection.
 
Hi John,

Thank you for the reply.

I had the httpwebrequest.timeout part working.
i.e. if it didn't connect within the specified time, then the connection would time out.

I had a try / catch block as well, but it was the status property that i was unable to find.

Presumably, i'd gotten lazy and hadn't read to the very end of the documentation where it explained about the webexception.status.



Sorry, but thank you very much for your assistance.
 
Hi John,

Thank you for the reply.

I had the httpwebrequest.timeout part working.
i.e. if it didn't connect within the specified time, then the connection would time out.

I had a try / catch block as well, but it was the status property that i was unable to find.

Presumably, i'd gotten lazy and hadn't read to the very end of the documentation where it explained about the webexceptionstatus.



Sorry, but thank you very much for your assistance.

use try / catch block
but instead of catch ex as Exception use catch ex as WebException
hope that helps
 
Back
Top