Frode Lillerud
Frode Lillerud

Reputation: 7394

Get requested URL from the Response of System.Net.WebClient?

I'm using the System.Net.WebClient to retrieve some data from a 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?
}

The Requests I send will contain a URL with some parameters that change for each call, and I need to know which parameter I asked for when the reponse comes back.

Can I for instance use the .UserState property of the sender object?

Upvotes: 1

Views: 2516

Answers (1)

Paulo Santos
Paulo Santos

Reputation: 11587

Well, if the URL is required, I'd change your code to something like this:

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;
}

Upvotes: 6

Related Questions