tags:

views:

20

answers:

2

Hey all

Well basically I want to record the uri which produces an error when testing, using the debugger I can find the uri which failed, however I don't know how to retrieve it, here's a print screen below

http://img802.imageshack.us/img802/5465/progps.jpg

Advice appreciated.

+1  A: 
(e.Error.Response as HttpWebResponse).ResponseUri
James Curran
But won't ResponseUri be different from the requested Uri if there are redirects?
Jim Mischel
A: 

Rather than call WebClient.DownloadStringAsync(Uri), call the overload, DownloadString(Uri, Object), passing the Uri as the second parameter. Then, in the event handler, you can cast the value of e.UserToken to Uri to retrieve the value. That is:

Uri uri = new Uri("http://example.com");
WebClient client = new WebClient();
client.DownloadStringCompleted = StringDownloaded;
client.DownloadStringAsync(uri, uri);


void StringDownloaded(object sender, DownloadStringCompletedEventArgs e)
{
    Uri uri = (Uri)e.UserToken;

    ...
}

You can use this technique to pass any kind of state to the event handler.

Jim Mischel
Ash