最近在做一个爬虫项目,需要抓取一些网站的资讯和其中的图片,抓取过程中发现有一些图片链接会重定向,而服务器存储时不支持重定向,于是想尝试直接拿到重定向的链接。
借助Chrome浏览器的开发者工具(快捷键F12),选择Network -> Headers,可以看到其重定向后的地址在Response Headers中的Location属性中。
查阅资料文档可知,
1.类HttpURLConnection提供了如下方法:
public void setInstanceFollowRedirects(boolean followRedirects)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance.
The default value comes from followRedirects, which defaults to true.
Parameters:
followRedirects a boolean indicating whether or not to follow HTTP redirects.
2.其父类URLConnection有如下方法:
public String getHeaderField(String name)
Returns the value of the named header field.
If called on a connection that sets the same header multiple times with possibly different values, only the last value is returned.
Parameters: