簡體   English   中英

如何在 Java 中獲取 HTTP 錯誤 400 上的響應 json

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

我正在使用 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();
    }

當發生錯誤時,我只有一個錯誤的請求 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)

但是,當我通過 POSTMAN 發出相同的請求時,它會准確返回錯誤內容,例如:

{ "profile": { "email": "必須是有效的電子郵件地址" } }

在此處輸入圖片說明

如何在我的 java 代碼中獲得相同的錯誤信息?

謝謝。

推薦你使用ok-http

這是一些有用的鏈接。

http post 請求(json 數據)

從響應中獲取錯誤消息

如果你只想使用HttpsURLConnection 。就像@Smile 說的,使用conn.getErrorStream()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM