Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection读取403上的响应内容时出错_Java_Http_Http Status Code 403 - Fatal编程技术网

Java HttpURLConnection读取403上的响应内容时出错

Java HttpURLConnection读取403上的响应内容时出错,java,http,http-status-code-403,Java,Http,Http Status Code 403,当我使用403响应从URL获取数据时 is = conn.getInputStream(); 它抛出IOException,我无法获取响应数据 但是,当我使用firefox并直接访问该url时,响应代码仍然是403,但我可以获取html内容。根据javadocs,该方法将返回一个可用于从错误条件(如404)中检索数据的值。尝试以下操作: try { String text = "url"; URL url = new URL(text); URLConnection c

当我使用403响应从URL获取数据时

is = conn.getInputStream();
它抛出IOException,我无法获取响应数据


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

根据javadocs,该方法将返回一个可用于从错误条件(如404)中检索数据的值。

尝试以下操作:

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();
}

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();
}
试试这个:

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

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

这非常有效!不,不会,因为函数的代码只包含“returnnull;”线路。(Java 6,7)@Gangnus仔细阅读Javadoc:“如果连接没有连接,或者如果服务器在连接时没有错误,或者如果服务器有错误但没有发送错误数据,此方法将返回null。这是默认值。”否则(错误4xx),您将获得要读取的流。@MiljenMikic代码和Javadoc之间的差异只意味着最后一个错误。@Gangnus HttpURLConnection是抽象类。具体实现的工作原理与Javadoc中的解释完全相同。@MiljenMikic它是抽象的。但正如我所说,这个函数已经存在了。不存在“缺少机具异常”。具体的实现在您编写的过程中起作用。您可以重写父函数,但不必重写。Javadoc是错误的,它会导致更多的错误。我认为它应该是
(code>=200)&&(code<300)
@slf你是对的。它实际上取决于实现,唯一的“官方”方法是检查
getErrorStream
是否返回null,但这只有在强制执行请求后才起作用。我更新代码以反映这一点。