简体   繁体   中英

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM