我通过Java访问网页如下:
URLConnection con = url.openConnection();
但在某些情况下,网址重定向到另一个网址。 所以,我想知道它以前的URL重定向的URL。
下面是我得到的响应头字段:
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]
因此,在目前,我正在构建从值重定向的URL Set-Cookie
报头字段。 在上述情况下,重定向的URL是copenhagen.craigslist.org
有没有通过,我能确定特定的URL会重定向到哪个URL任何标准方式。
我知道,当一个URL重定向到其他URL,服务器发送包含一个中间响应Location
报头字段,它告诉重定向的URL,但我不通过接收中间响应url.openConnection();
方法。
您需要将施放URLConnection
到HttpURLConnection
,并指示它无法通过设置跟随重定向HttpURLConnection#setInstanceFollowRedirects()
以false
。 您还可以通过设置全局HttpURLConnection#setFollowRedirects()
你只需要处理重定向自己,那么。 通过检查响应代码HttpURLConnection#getResponseCode()
抢Location
由头部URLConnection#getHeaderField()
然后火就可以了新的HTTP请求。
简单地调用的URLConnection实例的getURL()调用的getInputStream()后:
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();
如果您需要了解重定向是否发生过真正得到它的内容,这里是示例代码:
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 );
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;
}
为了自己获得“ 用户代理 ”和“ 引荐 ”,就到你安装的浏览器(谷歌浏览器如按F12)之一的开发者模式。 然后去标签“网络”,然后点击请求之一。 你应该看到它的细节。 只需按下“头”子选项卡(如下图)
看一看在HttpURLConnection
类API文档 ,特别是setInstanceFollowRedirects()
其实我建议使用固体开源库作为HTTP客户端。 如果你看看http客户端通过ASF,你会发现生活变得更加简单。 这是HTTP一个易于使用,可扩展性和强大的客户端。
@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);