如何从HttpClient获取最后一个URL? [英] How to get last url from HttpClient?
问题描述
好的,我最近切换到了.NET Framework 4.5,并开始使用HttpClient而不是HttpWebRequest&回复. 我真的很喜欢这种异步/等待样式,但是我不知道在POST/GET请求后如何获取重定向的URL.
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.
通过HttpWebResponse,我可以使用.ResponseUri属性
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;
带我进行了3个小时的搜索,但仍然无法完成:(
Took me 3 hours of searching and i still can't get it done:(
推荐答案
So from the msdn articles HttpResponseMessage returns as a Task from an HttpClient call.
此HttpResponseMessage具有名为请求消息的属性,它具有名为 RequestUri 的属性,请确保在属性中查找此链接的部分.
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.
示例代码:
// 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);
这篇关于如何从HttpClient获取最后一个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!