简体   繁体   English

如何在 Java 中获取 HTTP 错误 400 上的响应 json

[英]How to get the response json on HTTP error 400 in Java

I'm making a request using HttpUrlConnection (POST):我正在使用 HttpUrlConnection (POST) 发出请求:

try
    {

        String v_Authorization="";
        String v_MessageResponseCode="";

        switch (in_TipoAuthorization) {

            case 1: //No Auth  
                v_Authorization = in_Authorization;
                break;

            case 2: //Basic Auth
                //...
                break;

            case 3: //Bearer Token
                //...
                break;

            case 4: //OAuth2 Bearer Token
                //...
                break;

        }

        URL url = new URL(in_URL);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        conn.setRequestProperty("Authorization", v_Authorization);
        conn.setRequestProperty("Content-Type", in_ContentType);
        conn.setRequestProperty("Aceept", in_Accept);       
        conn.setRequestMethod(in_Method);
        conn.setDoOutput(true);
        conn.setDoInput(true);

        OutputStream os = conn.getOutputStream();
        os.write(in_JsonFile.getBytes("UTF-8"));
        os.flush();    
        os.close();

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder builderRetorno = new StringBuilder();

        String retorno="";
        while ((retorno = br.readLine()) != null) {
            if (in_ImportCsvFile == 1) {
                builderRetorno.append(retorno + "\n");
            } else {
                builderRetorno.append(retorno);
            }
        }

        out_HTTP = builderRetorno.toString();
        out_getResponseCode = conn.getResponseCode();


      } catch(MalformedURLException ex){
            logInfo(ex.toString());
            ex.printStackTrace();
            out_HTTP = ex.toString();

        } catch(IOException e) {
            logInfo(e.toString());
            e.printStackTrace();
            out_HTTP = e.toString();

      } finally {
          conn.disconnect();
    }

When an error occurs I only have a Bad Request 400 return and e.printStackTrace does not tell me exactly what the error is:当发生错误时,我只有一个错误的请求 400 返回,而 e.printStackTrace 并没有告诉我确切的错误是什么:

    java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:83)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:437)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1956)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1951)
    at java.security.AccessController.doPrivileged(AccessController.java:703)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1950)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1520)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
    at com.ibm.net.ssl.www2.protocol.https.b.getInputStream(b.java:91)
    at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1504)
Caused by: 
java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1906)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:491)
    at com.ibm.net.ssl.www2.protocol.https.b.getResponseCode(b.java:56)
    at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1493)

However, when I make the same request via POSTMAN, it returns exactly what the error is, example:但是,当我通过 POSTMAN 发出相同的请求时,它会准确返回错误内容,例如:

{ "profile": { "email": "must be a valid email address" } } { "profile": { "email": "必须是有效的电子邮件地址" } }

enter image description here在此处输入图片说明

How do I get this same error information in my java code?如何在我的 java 代码中获得相同的错误信息?

Thanks.谢谢。

Recommend you use ok-http .推荐你使用ok-http

Here is some helpful link.这是一些有用的链接。

http post request (json data) http post 请求(json 数据)

get an error message from response从响应中获取错误消息

If you only want to use HttpsURLConnection .Like @Smile says, use conn.getErrorStream()如果你只想使用HttpsURLConnection 。就像@Smile 说的,使用conn.getErrorStream()

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

相关问题 如何使用Jersey Client将HTTP 400响应解析为Java类? - How to parse HTTP 400 response using Jersey Client into a Java Class? Spotify创建播放列表Java-响应代码400,“ JSON解析错误” - Spotify Create Playlist Java - Response Code 400, “Error parsing JSON” 在 Android 中获取 400 HTTP 响应的响应正文? - Get response body on 400 HTTP response in Android? Java GDAX经过身份验证的REST请求HTTP GET错误400 - Java GDAX Authenticated REST Request HTTP GET Error 400 如何在Java中将http响应捕获为Json对象 - How to capture the http response as a Json Object in Java 如何在Java中操作HTTP json响应数据 - how to manipulate HTTP json response data in Java 使用Java中的JSON到Bitbucket服务器的HTTP POST返回400作为响应代码 - HTTP POST using JSON in java to bitbucket server returns 400 as response code 如何找出Java中400 Http错误的详细信息? - How to find out specifics of 400 Http error in Java? 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser Java-当我尝试获取源代码服务器时返回的HTTP响应代码:400 - Java - when I tried to get the source code server returned HTTP response code: 400
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM