【发布时间】:2016-02-21 08:39:28
【问题描述】:
我想知道为什么这个示例代码:
public class Test {
public static void main(String[] args) throws IOException {
testLink("http://google.com");
testLink("http://stackoverflow.com");
testLink("http://docs.oracle.com");
}
private static void testLink(String urlStr) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
System.out.println(conn.getResponseCode());
conn.disconnect();
}
}
通常打印:
200
403
200
而不是:
200
200
200
【问题讨论】:
-
SO 不允许连接使用的
User-Agent。 -
403 错误表示禁止权限被拒绝。
-
我知道 403 是什么意思 :-) 原因是 'User-Agent'