Java - 如何找到网址的重定向网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2659000/
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
Java - How to find the redirected url of a url?
提问by Yatendra Goel
I am accessing web pages through java as follows:
我通过java访问网页如下:
URLConnection con = url.openConnection();
But in some cases, a url redirects to another url. So I want to know the url to which the previous url redirected.
但在某些情况下,一个 url 会重定向到另一个 url。所以我想知道上一个url重定向到的url。
Below are the header fields that I got as a response:
以下是我作为响应得到的标题字段:
null-->[HTTP/1.1 200 OK]
Cache-control-->[public,max-age=3600]
last-modified-->[Sat, 17 Apr 2010 13:45:35 GMT]
Transfer-Encoding-->[chunked]
Date-->[Sat, 17 Apr 2010 13:45:35 GMT]
Vary-->[Accept-Encoding]
Expires-->[Sat, 17 Apr 2010 14:45:35 GMT]
Set-Cookie-->[cl_def_hp=copenhagen; domain=.craigslist.org; path=/; expires=Sun, 17 Apr 2011 13:45:35 GMT, cl_def_lang=en; domain=.craigslist.org; path=/; expires=Sun, 17 Apr 2011 13:45:35 GMT]
Connection-->[close]
Content-Type-->[text/html; charset=iso-8859-1;]
Server-->[Apache]
So at present, I am constructing the redirected url from the value of the Set-Cookie
header field. In the above case, the redirected url is copenhagen.craigslist.org
所以目前,我正在从头Set-Cookie
字段的值构造重定向的 url 。在上述情况下,重定向的 url 是copenhagen.craigslist.org
Is there any standard way through which I can determine which url the particular url is going to redirect.
是否有任何标准方法可以确定特定 url 将重定向哪个 url。
I know that when a url redirects to other url, the server sends an intermediate response containing a Location
header field that tells the redirected url but I am not receiving that intermediate response through the url.openConnection();
method.
我知道当 url 重定向到其他 url 时,服务器会发送一个中间响应,其中包含一个Location
标头字段,该字段告诉重定向的 url,但我没有通过该url.openConnection();
方法收到该中间响应。
采纳答案by BalusC
You need to cast the URLConnection
to HttpURLConnection
and instruct it to notfollow the redirects by setting HttpURLConnection#setInstanceFollowRedirects()
to false
. You can also set it globally by HttpURLConnection#setFollowRedirects()
.
您需要将 转换URLConnection
为HttpURLConnection
并通过设置为指示它不跟随重定向。您也可以通过.HttpURLConnection#setInstanceFollowRedirects()
false
HttpURLConnection#setFollowRedirects()
You only need to handle redirects yourself then. Check the response code by HttpURLConnection#getResponseCode()
, grab the Location
header by URLConnection#getHeaderField()
and then fire a new HTTP request on it.
你只需要自己处理重定向。检查响应代码 by HttpURLConnection#getResponseCode()
,获取Location
标头 by URLConnection#getHeaderField()
,然后在其上触发新的 HTTP 请求。
回答by b_erb
Have a look at the HttpURLConnection
class API documentation, especially setInstanceFollowRedirects()
.
查看HttpURLConnection
类API 文档,尤其是setInstanceFollowRedirects()
.
回答by Raymond Kroeker
I'd actually suggest using a solid open-source library as an http client. If you take a look at http clientby ASF you'll find life a lot easier. It is an easy-to-use,scalable and robust client for http.
我实际上建议使用可靠的开源库作为 http 客户端。如果您查看ASF 的http 客户端,您会发现生活容易得多。它是一个易于使用、可扩展且健壮的 http 客户端。
回答by amobiz
Simply call getUrl() on URLConnection instance after calling getInputStream():
调用 getInputStream() 后,只需在 URLConnection 实例上调用 getUrl():
URLConnection con = new URL( url ).openConnection();
System.out.println( "orignal url: " + con.getURL() );
con.connect();
System.out.println( "connected url: " + con.getURL() );
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
is.close();
If you need to know whether the redirection happened before actually getting it's contents, here is the sample code:
如果您需要在实际获取内容之前知道重定向是否发生,这里是示例代码:
HttpURLConnection con = (HttpURLConnection)(new URL( url ).openConnection());
con.setInstanceFollowRedirects( false );
con.connect();
int responseCode = con.getResponseCode();
System.out.println( responseCode );
String location = con.getHeaderField( "Location" );
System.out.println( location );
回答by franzu
@balusC I did as you wrote . In my case , I've added cookie information to be able to reuse the session .
@balusC 我照你写的做了。就我而言,我添加了 cookie 信息以便能够重用会话。
// get the cookie if need
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
回答by Mohsen Abasi
public static URL getFinalURL(URL url) {
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setInstanceFollowRedirects(false);
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
con.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.addRequestProperty("Referer", "https://www.google.com/");
con.connect();
//con.getInputStream();
int resCode = con.getResponseCode();
if (resCode == HttpURLConnection.HTTP_SEE_OTHER
|| resCode == HttpURLConnection.HTTP_MOVED_PERM
|| resCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String Location = con.getHeaderField("Location");
if (Location.startsWith("/")) {
Location = url.getProtocol() + "://" + url.getHost() + Location;
}
return getFinalURL(new URL(Location));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return url;
}
To get "User-Agent" and "Referer" by yourself, just go to developer mode of one of your installed browser (E.g. press F12 on Google Chrome). Then go to tab 'Network' and then click on one of the requests. You should see it's details. Just press 'Headers' sub tab (the image below)
要自己获得“ User-Agent”和“ Referer”,只需进入您安装的浏览器之一的开发者模式(例如在谷歌浏览器上按F12)。然后转到“网络”选项卡,然后单击其中一个请求。你应该看到它的细节。只需按“标题”子选项卡(下图)