Suppose I create a HTTPWebRequest, call its GetResponse() and start reading from the response stream. If the connection is interrupted while reading from the stream, do I have to wait for it to time out, or can I know right away that something’s gone wrong? No exception is thrown when I interrupt the connection (e.g. I disconnect my computer from the network).
Suppose I create a HTTPWebRequest , call its GetResponse() and start reading from the
Share
It depends on the situation.
In general you’ll need to be prepared for both situations (immediate and late interruption).
If, for example, the server disconnects you, you’ll know relatively quickly.
See http://msdn.microsoft.com/en-us/library/system.net.webexceptionstatus for the kinds of errors that can occur (the
WebRequestclasses throwWebExceptions on errors)You have a variety of options:
BeginGet...andEndGet...) and model your application around this. Basically you’ll be notified “at some point” if there was a success or error. Do something else in the meantimeReadTimeouton the acquired stream (See comment on the other answer, setTimeouton the request as well). Re-try or whatever.