1

In this walkthrough:

       http://msdn.microsoft.com/en-us/library/vstudio/hh300224.aspx

how would the asynchronous approach be modified to handle timeout scenarios?

     using (WebResponse response = await req.GetResponseAsync())
     {

     }

Or would a different approach have to be taken?

3 Answers 3

5

The easiest way to do async HTTP requests is to use HttpClient rather than WebRequest, and HttpClient has a Timeout property you can set.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a walkthrough or blog post you might suggest, Stephen? I've been going in circles on this for several days, running into deprecated solutions that don't take advantage of C#5.0. I'm hoping to find an example that performs an async web request, handles timeout, and updates the UI after each such request with a status (good, bad, timed-out). Eventually I want to issue such requests in a tight loop, so that many of them can be active concurrently. I'm checking a database of 100K urls to see which ones are good/moved/slow.
I'm not aware of any resource like that. It seems like it wouldn't be hard to program, though; just define an async helper method to handle the exceptions and translate them to an appropriate return value.
The advantage I saw to the WebRequest is that it has a HEAD method, and I couldn't find the counterpart to that in the HttpClient. But I've since found it: social.msdn.microsoft.com/Forums/windowsapps/en-US/…
0

Set the Timeout property on the WebRequest object

3 Comments

Oh, that is sad - my bad :(
But i'll keep my answer here so that no one else makes same mistake.
0

Follow a solution to respect the timeout

await Task.Run(() => {
   var varHttpResponse = varWebRequest.GetResponse();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.