从HttpWebResponse重定向的URL集合 [英] Get a collection of redirected URLs from HttpWebResponse
问题描述
我想检索表示到 URL是
从 URL X
所采取的路径URL列表。其中, X
可能会被重定向几次
例如:
块引用>
这会重定向到:
< A HREF =http://www.example.com/bar相对=nofollow> http://www.example.com/bar
块引用>
,然后重定向到:
的 http://www.example.com/foobar
块引用>
有的方式获得响应对象作为字符串此重定向路径:
http://www.example.com/foo> http://www.example.com/bar> http://www.example.com/foobar
我通过我能够得到的最终网址
ResponseUri
例如:公共静态字符串GetRedirectPath(字符串URL)
{
StringBuilder的SB =新的StringBuilder();
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);使用
(VAR响应=(HttpWebResponse)request.GetResponse())
{
sb.Append(response.ResponseUri);
}
返回sb.ToString();
}
不过,这显然跳过的URL。似乎没有一种简单的方法(或方法呢?),以获得完整的路径?
解决方案有一个方式:
公共静态字符串RedirectPath(字符串URL)
{
StringBuilder的SB =新的StringBuilder() ;
线位置= string.Copy(URL); (!string.IsNullOrWhiteSpace(位置))
,而
{
sb.AppendLine(位置); //你也可以用'附加'
HttpWebRequest的要求= HttpWebRequest.CreateHttp(位置);
request.AllowAutoRedirect = FALSE;使用
(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
{
位置= response.GetResponseHeader(位置);
}
}
返回sb.ToString();
}
我这个TinyURL的测试它:的http://tinyurl.com/google 结果
输出:
<预类=郎无prettyprint -override>http://tinyurl.com/google
http://www.google.com/
http://www.google.be/?gws_rd= CR
按任意键继续。 。 。
这是正确的,因为我的TinyURL的重定向到google.com(点击此处查看:的 http://preview.tinyurl.com/google ),和google.com重定向我google.be,因为我在比利时来的。
I'm trying to retrieve a list of urls that represent the path taken from
URL X
toURL Y
whereX
may be redirected several times.For example:
That will redirect to:
Which then redirects to:
Is there a way of get this redirect pathway from the response object as a string:
http://www.example.com/foo > http://www.example.com/bar > http://www.example.com/foobar
I'm able to get at the final URL via
ResponseUri
e.g.public static string GetRedirectPath(string url) { StringBuilder sb = new StringBuilder(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); using (var response = (HttpWebResponse)request.GetResponse()) { sb.Append(response.ResponseUri); } return sb.ToString(); }
But this obviously skips the URL in between. There doesn't seem be an easy way (or way at all?) to get the full pathway?
解决方案There is a way:
public static string RedirectPath(string url) { StringBuilder sb = new StringBuilder(); string location = string.Copy(url); while (!string.IsNullOrWhiteSpace(location)) { sb.AppendLine(location); // you can also use 'Append' HttpWebRequest request = HttpWebRequest.CreateHttp(location); request.AllowAutoRedirect = false; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { location = response.GetResponseHeader("Location"); } } return sb.ToString(); }
I tested it with this TinyURL: http://tinyurl.com/google
Output:http://tinyurl.com/google http://www.google.com/ http://www.google.be/?gws_rd=cr Press any key to continue . . .
This is correct, because my TinyURL redirects you to google.com (check it here: http://preview.tinyurl.com/google), and google.com redirects me to google.be, because I'm in Belgium.
这篇关于从HttpWebResponse重定向的URL集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!