我通过java访问网页如下:
URLConnection con = url.openConnection();
但在某些情况下,网址会重定向到另一个网址.所以我想知道前一个网址重定向的网址.
以下是我作为回复得到的标题字段:
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]
所以目前,我正在从头Set-Cookie
字段的值构建重定向的URL .在上面的例子中,重定向的URL是copenhagen.craigslist.org
是否有任何标准方法可以确定特定网址将重定向到哪个网址.
我知道当一个url重定向到其他url时,服务器会发送一个包含Location
头字段的中间响应,该头字段告诉重定向的url但我没有通过该url.openConnection();
方法接收到该中间响应.
在调用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();
如果你需要知道重定向是否在实际获取它的内容之前发生,这里是示例代码:
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 );
您需要转换URLConnection
为HttpURLConnection
并指示它不通过设置HttpURLConnection#setInstanceFollowRedirects()
为重定向false
.您也可以通过全局设置它HttpURLConnection#setFollowRedirects()
.
您只需要自己处理重定向.检查响应代码HttpURLConnection#getResponseCode()
,获取Location
标题URLConnection#getHeaderField()
,然后在其上触发新的HTTP请求.
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; }
要自己获得" User-Agent "和" Referer ",只需转到已安装浏览器的开发者模式(例如,在Google Chrome上按F12).然后转到"网络"标签,然后单击其中一个请求.你应该看到它的细节.只需按"标题"子标签(下图)