我正在使用System.Net.WebClient从URL中检索一些数据。
void GetAirportData()
{
var url = "http://server.example.com/airports.xml?id=OSL";
var webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(url, UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//How can I see the original URL here, so that I can see which id parameter was passed?
}
我发送的请求将包含一个URL,其中包含一些参数,这些参数会针对每个调用进行更改,我需要知道响应响应时我询问的参数。
我可以使用发件人对象的.UserState属性吗?
答案 0 :(得分:6)
好吧,如果需要这个网址,我会将您的代码更改为以下内容:
void GetAirportData()
{
var url = new Uri("http://server.example.com/airports.xml?id=OSL", UriKind.Absolute);
var webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(url, url);
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// Gets the original url passed.
var url = e.UserState as Uri;
}