在Java中检索给定URL的最终位置 [英] Retrieve the final location of a given URL in Java
问题描述
我正在尝试检索给定URL的最终位置(String ref),如下所示:
HttpURLConnection con =( HttpURLConnection)新的URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty(User-Agent,);
int responseCode = con.getResponseCode();
返回con.getURL()。toString();
它适用于大多数情况,但很少返回包含另一个重定向的URL。
我在这里做错了什么?
为什么即使在调用setInstanceFollowRedirects(true)后我也得到responseCode = 3xx? / p>
更新:
好的,responseCode有时可能是3xx。
如果它发生,那么我将返回con.getHeaderField(Location)。
现在的代码是:
HttpURLConnection con =(HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty(User-Agent,);
int responseType = con.getResponseCode()/ 100;
while(responseType == 1)
{
Thread.sleep(10);
responseType = con.getResponseCode()/ 100;
}
if(responseType == 3)
return con.getHeaderField(Location);
返回con.getURL()。toString();
如果有人发现上述代码有任何问题,请发表评论。
更新
- 删除了代码1xx的处理,正如大多数评论者所说的那样没有必要。
-
在返回之前测试Location标头是否存在,以便处理代码304.
HttpURLConnection con =(HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty(User-Agent,);
if(con.getResponseCode()/ 100 == 3)
{
String target = con.getHeaderField(Location);
if(target!= null)
返回目标;
}
返回con.getURL()。toString();
如果协议更改,HttpURLConnection将不会遵循重定向,例如http到https或https到http。在这种情况下,它将返回3xx代码,您应该能够获得Location标头。如果新网址也重定向,您可能需要再次打开连接。所以基本上,当你得到一个非重定向响应代码时,使用一个循环并打破它。另外,请注意无限重定向循环,您可以设置迭代次数的限制或检查是否已经访问过每个新URL。
I am trying to retrieve the final location of a given URL (String ref) as follows:
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent","");
int responseCode = con.getResponseCode();
return con.getURL().toString();
It works in most cases, but rarely returns a URL which yet contains another redirection.
What am I doing wrong here?
Why do I get responseCode = 3xx, even after calling setInstanceFollowRedirects(true)?
UPDATE:
OK, responseCode can sometimes be 3xx.
If it happens, then I will return con.getHeaderField("Location") instead.
The code now is:
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent","");
int responseType = con.getResponseCode()/100;
while (responseType == 1)
{
Thread.sleep(10);
responseType = con.getResponseCode()/100;
}
if (responseType == 3)
return con.getHeaderField("Location");
return con.getURL().toString();
Will appreciate comment should anyone see anything wrong with the code above.
UPDATE
- Removed the handling of code 1xx, as according to most commenters it is not necessary.
Testing if the Location header exists before returning it, in order to handle code 304.
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection(); con.setInstanceFollowRedirects(true); con.setRequestProperty("User-Agent",""); if (con.getResponseCode()/100 == 3) { String target = con.getHeaderField("Location"); if (target != null) return target; } return con.getURL().toString();
HttpURLConnection will not follow redirects if the protocol changes, such as http to https or https to http. In that case, it will return the 3xx code and you should be able to get the Location header. You may need to open a connection again in case that new url also redirects. So basically, use a loop and break it when you get a non-redirect response code. Also, watch out for infinite redirect loops, you could set a limit for the number of iterations or check if each new url has been visited already.
这篇关于在Java中检索给定URL的最终位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!