【问题标题】:HttpURLConnection reading response content on 403 error [duplicate]HttpURLConnection读取403错误的响应内容[重复]
【发布时间】:2011-06-05 16:55:53
【问题描述】:

当我从带有 403 响应的 URL 获取数据时

is = conn.getInputStream();

它抛出一个 IOException,我无法获取响应数据。

但是当我使用firefox并直接访问该url时,ResponseCode仍然是403,但我可以获取html内容

【问题讨论】:

    标签: java http http-status-code-403


    【解决方案1】:

    根据 javadocs,HttpURLConnection.getErrorStream 方法将返回一个 InputStream,它可用于从错误条件(例如 404)中检索数据。

    【讨论】:

    • 不,不会,因为函数的代码只包含'return null;'线。 (Java 6,7)
    • @Gangnus 仔细阅读Javadoc:“如果连接没有连接,或者服务器在连接时没有错误,或者如果服务器有错误但没有发送错误数据,这个方法将返回 null。这是默认值。”否则(错误 4xx),您将获得要读取的流。
    • @MiljenMikic code和Javadoc的区别只是最后一个是错误的。
    • @Gangnus HttpURLConnection 是 abstract 类。具体实现完全按照 Javadoc 中的说明进行。
    • @MiljenMikic 这是抽象的。但正如我所说,这个功能已经存在。没有“缺少执行异常”。具体实现在您编写时起作用。您可以覆盖父母的功能,但您不需要。 Javadoc 是错误的,会导致更多错误。
    【解决方案2】:

    HttpURLConnection的使用示例:

    String response = null;
    try {
        URL url = new URL("http://google.com/pagedoesnotexist");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
        // Hack to force HttpURLConnection to run the request
        // Otherwise getErrorStream always returns null
        connection.getResponseCode();
        InputStream stream = connection.getErrorStream();
        if (stream == null) {
            stream = connection.getInputStream();
        }
        // This is a try with resources, Java 7+ only
        // If you use Java 6 or less, use a finally block instead
        try (Scanner scanner = new Scanner(stream)) {
            scanner.useDelimiter("\\Z");
            response = scanner.next();
        }
    } catch (MalformedURLException e) {
        // Replace this with your exception handling
        e.printStackTrace();
    } catch (IOException e) {
        // Replace this with your exception handling
        e.printStackTrace();
    }
    

    【讨论】:

    • 我以为一定是(code >= 200) && (code < 300)
    • @slf 你是对的。它实际上取决于实现,唯一的“官方”方法是检查 getErrorStream 是否返回 null,但这仅在强制执行请求后才有效。我更新了我的代码以反映这一点。
    【解决方案3】:

    试试这样的:

    try {
        String text = "url";
        URL url = new URL(text);
        URLConnection conn = url.openConnection();
        // fake request coming from browser
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String f = in.readLine();
        in.close();
        System.out.println(f);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 这就像一个魅力!
    【解决方案4】:

    试试这个:

    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getResponseCode() / 100 == 2 ? con.getInputStream() : con.getErrorStream()));
    

    来源 https://stackoverflow.com/a/30712213/505623

    【讨论】:

      【解决方案5】:

      即使添加了代理字符串,我也遇到了同样的错误。经过一天的调查,终于找到了问题所在。如果 url 方案以“HTTPS”开头,这真的很奇怪,它会导致错误 403。它应该是小写的(“https”)。因此,请确保在打开连接之前调用“url.toLowercase()”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        相关资源
        最近更新 更多