当我通过Java运行时,下面的url给出响应代码301. url是: http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M 它根据java代码给出的重定向url是:
http://www.amazon.com/Kindle-Keyboard-Free-Wi-Fi-Display/dp/B004HZYA6E
代码是:
public class newTestURLConnection {
public static void main(String[] args) throws IOException {
String url="http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M";
HttpURLConnection con =(HttpURLConnection) new URL( url ).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
InputStream is = con.getInputStream();
System.out.println(con.getHeaderField("Location"));
System.out.println( "Response code: " + con.getResponseCode() );
}
}
但是当我在浏览器中打开网址时,它不会重定向到:http://www.amazon.com/Kindle-Keyboard-Free-Wi-Fi-Display/dp/B004HZYA6E。谁能提出为什么会出现这种差异?
答案 0 :(得分:1)
instanceFollowRedirects
protected boolean instanceFollowRedirects
If true, the protocol will automatically follow redirects. If false, the protocol will not automatically follow redirects.
This field is set by the setInstanceFollowRedirects method. Its value is returned by the getInstanceFollowRedirects method.
Its default value is based on the value of the static followRedirects at HttpURLConnection construction time.
答案 1 :(得分:0)
有人能说出这种差异的原因吗?
可能是亚马逊根据请求标头中的差异做出了不同的响应;例如用户代理标头中发送的内容。
这可能是为了向真实客户和网络抓取工具/内容抓取工具展示不同的结果。
无论是什么原因,这两页之间的区别非常微妙......至少对人眼而言。
如果这不仅仅是一次性的事情(例如测试一些代码),我还应该问你是否已阅读亚马逊“使用条件”;例如它所说的位:
“未经亚马逊明确书面同意,不得出于任何商业目的复制,复制,复制,出售,转售,访问或以其他方式利用任何亚马逊服务或任何亚马逊服务的任何部分。“