2012-12-29 83 views
0

如何在DownloadStringAsync之后获取DownloadStringCompletedEventHandler中的url?C# - 异步Webclient - 获取URL

我想尽可能快地阅读一堆网址。 我想使用一组Web客户端,但我需要在我的事件句柄来识别URL。我做这是处理返回的HTML代码的唯一方法。

+0

我们可以看到什么样的代码目前你有,给我们一个URL例如,如果您使用的是.NET 4.5,你可以使用,我们可以尝试我们的异体... – balexandre

+1

'字符串s =等待wc.DownloadStringTaskAsync(。 ...);' –

回答

1

当您调用DownLoadStringAsync时添加userstate。 另一个不太值得推荐的是反映到WebClient中以获取内部字段m_WebRequest。该对象拥有原始网址,但这可能会在新版本的框架中失败。

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