在java中,当请求失败时,如何获取http响应正文?

在java中,当请求失败时,如何获取http响应正文?,java,http,httpurlconnection,Java,Http,Httpurlconnection,我正在寻找一种方法,当请求以状态400结束时,接收响应主体。 我现在使用java.net建立http连接 以下是我正在使用的代码: InputStream response = new URL("http://localhost:8888/login?token=token").openStream(); try (Scanner scanner = new Scanner(response)) { String responseBody = scanner.useD

我正在寻找一种方法,当请求以状态400结束时,接收响应主体。 我现在使用java.net建立http连接

以下是我正在使用的代码:

InputStream response = new URL("http://localhost:8888/login?token=token").openStream();
try (Scanner scanner = new Scanner(response)) {
    String responseBody = scanner.useDelimiter("\\A").next();
    System.out.println(responseBody);
}
现在,我终于犯了这个错误

java.io.IOException:服务器返回了URL的HTTP响应代码:400

当我在浏览器中打开url时,它会显示一个json文件,表示出错的地方,但现在,我无法在java中收到该响应。

请尝试以下代码:

package com.abc.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String url = "http://localhost:8888/login?token=token";
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            int responseCode = connection.getResponseCode();
            InputStream inputStream;
            if (200 <= responseCode && responseCode <= 299) {
                inputStream = connection.getInputStream();
            } else {
                inputStream = connection.getErrorStream();
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

            StringBuilder response = new StringBuilder();
            String currentLine;

            while ((currentLine = in.readLine()) != null) 
                response.append(currentLine);

            System.out.println(response.toString());
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

}
package com.abc.test;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
公开课考试{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
试一试{
字符串url=”http://localhost:8888/login?token=token";
HttpURLConnection连接=(HttpURLConnection)新URL(URL).openConnection();
int responseCode=connection.getResponseCode();
输入流输入流;

如果(200我改变了我的方法,现在它起作用了

URL obj = new URL("http://localhost:8888/login?token=token");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);

System.out.println("Status Code : " + con.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getErrorStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
System.out.println("Response Body:");
System.out.println(response.toString());

实际上,当我获取响应输入流时,错误在第一行。@ArmanBabaei:尝试java类中添加的代码,看看通过JSON获取的响应对象。首先,我无法获取(InputStream response)。其次,InputStream似乎没有方法errorBody。@ArmanBabaei:用完整的行替换代码。如果可以的话,试试这个。@ArmanBabaei:谢谢,伙计!是的,看到你的方法了……是的,这就是我们可以继续的方法。你说的是什么意思“当我在浏览器中打开url时,它会显示一个json文件,表示出错的地方,但现在,我无法在java中收到该响应。"?您是否能够在浏览器中看到此给定URL的响应而无法获取java代码?@AmitVyas是的,我在浏览器中也获得了代码400,但这并不能阻止我获取响应。另一方面,在java中,它引发了一个异常,我无法获取响应。您不需要使用HttpUrlConenction。示例:URL URL=新URL(“”;HttpURLConnection con=(HttpURLConnection)url.openConnection();con.setRequestMethod(“GET”);con.setDoOutput(true);con.setRequestProperty(“内容类型”、“应用程序/json”);System.out.println(con.getResponseCode());System.out.println(con.getResponseMessage());我们同时达到相同答案的竞争条件:)