打开 URLConnection 后出现奇怪的状态 403

Strange status 403 after opening URLConnection

我想知道为什么这个示例代码:

public class Test {

    public static void main(String[] args) throws IOException {
        testLink("http://google.com");
        testLink("http://whosebug.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

根据 Wikipedia,403 响应通常表示以下两种情况之一:

Authentication was provided, but the authenticated user is not permitted to perform the requested operation.

The operation is forbidden to all users. For example, requests for a directory listing return code 403 when directory listing has been disabled.

所以很可能通过 User-Agent 连接到 SO 是被禁止的。