【问题标题】:Strange status 403 after opening URLConnection打开 URLConnection 后出现奇怪的状态 403
【发布时间】: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'

标签: java httpurlconnection


【解决方案1】:

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

提供了身份验证,但不允许经过身份验证的用户执行请求的操作。

禁止所有用户操作。例如,当目录列表被禁用时,请求目录列表返回代码 403。

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

【讨论】:

  • 显然 SO 不乐意收到没有用户代理的请求。放一个就很开心。
猜你喜欢
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2020-05-28
相关资源
最近更新 更多