husvar
husvar

Reputation: 403

C# - Async Webclient - Get URL

How can I get the url in the DownloadStringCompletedEventHandler after a DownloadStringAsync?

I am trying to read a bunch of urls as fast as possible. I am thinking of using using a set of Webclients but I need in my event handles to identify the url. I to do tht is the only way to process the returned html code.

Upvotes: 0

Views: 2998

Answers (1)

rene
rene

Reputation: 42494

add a userstate when you call the DownLoadStringAsync. One other less recommendable is to reflect into the WebClient to get the internal field m_WebRequest. That object holds the orginal Url, but this might fail in a new version of the framework.

    var wc = new WebClient();

    wc.DownloadStringCompleted += (sender, e) => 
    { 
        WebClient compWC = (WebClient) sender;
        string url = e.UserState as string;
        Console.WriteLine(compWC.ResponseHeaders[HttpResponseHeader.Server]);
        Console.WriteLine(url);
    };                

    wc.DownloadStringAsync(new Uri("http://www.google.nl"), "http://www.google.nl");

Upvotes: 1

Related Questions