views:

4065

answers:

4

How can I use HttpWebRequest (.NET, C#) asynchronously?

+3  A: 

Check out this article on Developer Fusion: http://www.developerfusion.com/code/4654/asynchronous-httpwebrequest/

Jeremy Reagan
+14  A: 

Use HttpWebRequest.BeginGetResponse()

HttpWebRequest webRequest;

void StartWebRequest()
{
    webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}

void FinishWebRequest(IAsyncResult result)
{
    webRequest.EndGetResponse(result);
}

The callback function is called when the asynchronous operation is complete. You need to at least call EndGetResponse() from this function.

Jon B
+1  A: 

You can also see the following, for a pretty complete example of doing what Jason is asking:

http://stuff.seans.com/2009/01/05/using-httpwebrequest-for-asynchronous-downloads/

Sean

Sean Sexton
A: 

use async

http://msdn.microsoft.com/en-us/library/system.net.webrequest.endgetrequeststream.aspx

Raj Kaimal
for a moment, I wondered if you were trying to comment on a recursive thread?
Kyle Hodgson
Copy and paste error. Fixed. Thanks Kyle!
Raj Kaimal