29

OK, i was recently switch to .NET framework 4.5 and start using HttpClient instead of HttpWebRequest & Response. I really love that async/await style but i don't know how to get the redirected url after a POST / GET request.

With HttpWebResponse i can use .ResponseUri attribute

HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("http://www.google.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
string responseURI = response.ResponseUri;

Took me 3 hours of searching and i still can't get it done:(

svick
  • 236,525
  • 50
  • 385
  • 514
aznboy84
  • 391
  • 1
  • 3
  • 5

1 Answers1

48

So from the msdn articles HttpResponseMessage returns as a Task from an HttpClient call.

This HttpResponseMessage has a property called Request Message, which has a property called RequestUri, make sure to look in the properties section of this link.

Sample Code:

// Create a New HttpClient object.
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://www.yahoo.com/");
response.EnsureSuccessStatusCode();
string responseUri = response.RequestMessage.RequestUri.ToString();
Console.Out.WriteLine(responseUri);
ermagana
  • 1,090
  • 6
  • 11
  • well, finally i can't use HttpClient at all, it's lacking too much basic function. i really appreciate if you can show me how to specific a source ip for HttpClient because my server have about 10 ip address and i want to use all of it – aznboy84 Jul 21 '13 at 08:09
  • @aznboy84 can you clarify your request? what goal are you trying to accomplish? – ermagana Jul 21 '13 at 15:30
  • some kind of bots, my target website has a limited client per ip address so i need to use multiple ip. Finnally i got HttpWebRequest work with async / await so i don't need HttpClient any more ^^! My old version already work fine but require too much CPU usage because of 500 threads so i think i should switch to Task style. Thanks alot ! – aznboy84 Jul 21 '13 at 23:07
  • @aznboy84 glad I could help, be sure to close out your question by either accepting my answer or submitting your answer to the question above :). – ermagana Jul 22 '13 at 01:27
  • 4
    HttpResponseMessage.RequestMessage refers to the request which led to the response message. HttpResponseMessage.RequestMessage.RequestUri refers to the uri that was requested not the last uri from which the response was redirected to. Reference: http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage.aspx – stormwild Sep 09 '14 at 04:21
  • 9
    If you are only interested in the redirect URL, and not the contents of the URL (the resource), then it is very important to add `HttpCompletionOption.ResponseHeadersRead` as the second parameter of the `GetAsync()` call. Otherwise, you will download the entire resource before being able to get the redirect URL -- imagine if the resource was a 1GB file! – Steve Guidi Sep 30 '14 at 20:16
  • 4
    Re: @stormwild 's comment - HttpResponseMessage.RequestMessage does refer to the same object used in the original request, however HttpClient will modify the HttpRequestMessage.RequestUri to the last uri fetched. – csauve Jan 13 '18 at 00:14