简体   繁体   English

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

[英]How can I get http response body when request is failed in java?

I'm looking for a way to receive the response body when a request ends up with the status 400. I'm now using java.net to make http connection.我正在寻找一种在请求以状态 400 结束时接收响应正文的方法。我现在使用 java.net 进行 http 连接。

Here is the code I'm using:这是我正在使用的代码:

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

Right now, I end up with this error现在,我最终遇到了这个错误

java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器返回 HTTP 响应代码:400 对于 ZE6B3091A8D2C4D552A4

When I open the url in my browser it shows me a json file representing what went wrong, but now, I can't receive that response in java.当我在浏览器中打开 url 时,它会显示一个 json 文件,表示出了什么问题,但现在,我无法在 java 中收到该响应。

Try the below code:试试下面的代码:

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

}

I changed my approach and now it works.我改变了我的方法,现在它起作用了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium java 读取 http 响应正文? - How can I read http response body using selenium java? 如何以字符串形式获取 HTTP 响应正文? - How can I get an HTTP response body as a string? 如果登录失败,如何获取 http 请求/响应的正文作为登录 spring 安全性 - How to get body of http request/response as log in spring security if login failed 如何在没有 BufferedReader 的情况下从 Java 中的 http get 请求中检索响应 - How can I retrieve the response from an http get request in Java WITHOUT BufferedReader 当 HTTP 请求返回状态为 401 时,如何在 Java 中解析响应正文 - How to parse the response body in Java, when the HTTP request has return status 401 如何在Java HTTP请求中获取代理响应? - How to get proxy response in java http request? Java HTTP 请求读取响应体 - Java HTTP request read response body 使用REST模板调用时,如何从HTTP删除方法获取Java中的响应正文(JSON) - How to get the response body (JSON) in Java from an HTTP delete method when called using the REST template 如何将HTTP响应正文解析为Java表? - How would I parse a HTTP response body into a table in Java? 如何使用restTemplate获取真正的http代码和响应正文? - How can I get real http code and response body using restTemplate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM