【问题标题】:How can I get http response body when request is failed in java?java中请求失败时如何获取http响应正文?
【发布时间】:2020-11-07 10:25:25
【问题描述】:

我正在寻找一种在请求以状态 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:服务器返回 HTTP 响应代码:URL 为 400

当我在浏览器中打开 url 时,它会显示一个 json 文件,表示出了什么问题,但现在,我无法在 java 中收到该响应。

【问题讨论】:

  • “当我在浏览器中打开 url 时,它会显示一个 json 文件,表示出了什么问题,但现在,我无法在 java 中收到该响应。”是什么意思?您是否能够在浏览器中看到此给定 URL 的响应而无法进入您的 java 代码?
  • @AmitVyas 是的,我在浏览器中也收到了代码 400,但这并不能阻止我收到响应。另一方面,在 java 中,它引发了一个异常,我无法得到响应。
  • 您不需要使用 HttpUrlConenction。示例: URL url = new URL("localhost:8888/login?token=token"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setDoOutput(true); con.setRequestProperty("Content-Type ", "应用程序/json"); System.out.println(con.getResponseCode()); System.out.println(con.getResponseMessage());
  • 比赛条件我们同时达到了相同的答案:)

标签: java http httpurlconnection


【解决方案1】:

试试下面的代码:

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

}

【讨论】:

  • 实际上错误在第一行,当我得到响应输入流时。
  • @ArmanBabaei :尝试在您的 java 类中添加的代码,并查看您通过 JSON 获得的响应对象。
  • 首先,我无法获得(InputStream 响应)。第二,InputStream好像没有errorBody这个方法。
  • @ArmanBabaei :用完整的行替换了代码。可以的话试试这个。
  • @ArmanBabaei:谢谢哥们!是的,看到了你的方法......是的,这就是我们可以继续的方式。
【解决方案2】:

我改变了我的方法,现在它可以工作了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2016-10-02
    • 1970-01-01
    相关资源
    最近更新 更多