Java Jsoup 获取重定向的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24907808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jsoup get redirected URL
提问by Sorter
I'm trying to fetch the actual(redirected) url from the one provided by a url shortener.
我正在尝试从网址缩短器提供的网址中获取实际(重定向)网址。
Let's take twitter url shortener for example. I'm able to get the response object also parsed it to get the document.
让我们以 Twitter 网址缩短器为例。我能够得到响应对象也解析它以获取文档。
Response response = Jsoup.connect("http://t.co/i5dE1K4vSs")
.followRedirects(true) //to follow redirects
.execute();
Now, considering a single redirect, where to get the final url from? Any method or strategy to achieve this?
现在,考虑单个重定向,从哪里获取最终 url?任何方法或策略来实现这一目标?
采纳答案by Syam S
The Response object has a url() method which should give you the final url. So you could do like
Response 对象有一个 url() 方法,它应该给你最终的 url。所以你可以这样做
String url = "http://t.co/i5dE1K4vSs";
Response response = Jsoup.connect(url).followRedirects(true).execute();
System.out.println(response.url())
If you want o get the intermediate redirects you should turn follow redirect off and then check for header "location". Eg
如果您想获得中间重定向,您应该关闭跟随重定向,然后检查标题“位置”。例如
String url = "http://t.co/i5dE1K4vSs";
Response response = Jsoup.connect(url).followRedirects(false).execute();
System.out.println(response.header("location"));
If it has multiple redirect you need to recurssively call the urls.
如果它有多个重定向,则需要递归调用 url。
回答by Anton
Code:
代码:
String originalUrl = Jsoup.connect("http://t.co/i5dE1K4vSs")
.followRedirects(true) //to follow redirects
.execute().url().toExternalForm();
System.out.println(originalUrl);
Output:
输出:
http://ibnlive.in.com/news/messi-considered-move-to-arsenal/487799-5-21.html
Explanation:
解释:
As the Connection.Response
has Connection.Base
as superinterface, you can just use the #url() method of it (and then use the URL
object as you want.
由于Connection.Response
具有Connection.Base
超级接口,您可以只使用它的 #url() 方法(然后URL
根据需要使用该对象。