views:

38

answers:

1

I'm using System.Net.WebClient to perform some HTTP operations in asynchronous mode. The reason to use asynchronous operations is, above anything else, the fact that I get progress change indications - which is only available for async operations, as stated by the docs.

So let's say I have my WebClient set up:

this.client = new WebClient();
this.client.UploadStringCompleted +=
    new UploadStringCompletedEventHandler(textUploadComplete);

and the delegate:

private void textUploadComplete(Object sender, UploadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // trigger UI failure notification
        return;
    }

    // FIXME not checking for response code == 200 (OK)
    // trigger UI success notification
}

So you see, I'm assuming that if no exception is raised, the requests was always successful (which may not be the case, since the HTTP response status code can be != 2xx). From the documentation on UploadFileAsync I can't tell if a non-200 response is processed as well.

I'm really new to C# and I can't seem to find a way to access the headers for that particular asynchronous request's response. Seems to me that each WebClient can only hold a response (or a set of headers) at any given time.

While I'm not going to be executing multiple parallel requests at the same time, I'd still like to know if there is a more elegant way of retrieving a particular request's response headers/status code, rather than having to get the "last-available" response from the WebClient.

Thanks.

+1  A: 

Check out this link on WebClients:

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

Also Check out this link too;

http://www.codeproject.com/KB/dotnet/WebClientAsyncDownloader.aspx

KhanZeeshan
The second link uses an approach I'd like to avoid: multiple instances of WebClient, one per request.
brunodecarvalho
After further investigating, it seems that WebClient does not support multiple concurrent requests, so if I want to perform parallel requests, I'll have to create multiple WebClient instances. Let's see if someone comes up with a better solution in a couple of hours; otherwise I'm marking your answer as correct.
brunodecarvalho