Read error response body in Java

后端 未结 8 1356
暗喜
暗喜 2020-11-28 05:32

In Java, this code throws an exception when the HTTP result is 404 range:

URL url = new URL(\"http://stackoverflow.com/asdf404notfound\");
HttpURLConnection          


        
相关标签:
8条回答
  • 2020-11-28 06:02

    Here is the bug report (close, will not fix, not a bug).

    Their advice there is to code like this:

    HttpURLConnection httpConn = (HttpURLConnection)_urlConnection;
    InputStream _is;
    if (httpConn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
        _is = httpConn.getInputStream();
    } else {
         /* error from server */
        _is = httpConn.getErrorStream();
    }
    
    0 讨论(0)
  • 2020-11-28 06:02
    InputStream is = null;
    if (httpConn.getResponseCode() !=200) {
        is = httpConn.getErrorStream();
    } else {
         /* error from server */
        is = httpConn.getInputStream();
    }
    
    0 讨论(0)
  • 2020-11-28 06:03

    I know that this doesn't answer the question directly, but instead of using the HTTP connection library provided by Sun, you might want to take a look at Commons HttpClient, which (in my opinion) has a far easier API to work with.

    0 讨论(0)
  • 2020-11-28 06:06

    It's the same problem I was having: HttpUrlConnection returns FileNotFoundException if you try to read the getInputStream() from the connection.
    You should instead use getErrorStream() when the status code is higher than 400.

    More than this, please be careful since it's not only 200 to be the success status code, even 201, 204, etc. are often used as success statuses.

    Here is an example of how I went to manage it

    ... connection code code code ...
    
    // Get the response code 
    int statusCode = connection.getResponseCode();
    
    InputStream is = null;
    
    if (statusCode >= 200 && statusCode < 400) {
       // Create an InputStream in order to extract the response object
       is = connection.getInputStream();
    }
    else {
       is = connection.getErrorStream();
    }
    
    ... callback/response to your handler....
    

    In this way, you'll be able to get the needed response in both success and error cases.

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 06:11

    My running code.

      HttpURLConnection httpConn = (HttpURLConnection) urlConn;    
     if (httpConn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
                            in = new InputStreamReader(urlConn.getInputStream());
                            BufferedReader bufferedReader = new BufferedReader(in);
                            if (bufferedReader != null) {
                                int cp;
                                while ((cp = bufferedReader.read()) != -1) {
                                    sb.append((char) cp);
                                }
                                bufferedReader.close();
                            }
                                in.close();
    
                        } else {
                            /* error from server */
                            in = new InputStreamReader(httpConn.getErrorStream());
                        BufferedReader bufferedReader = new BufferedReader(in);
                        if (bufferedReader != null) {
                            int cp;
                            while ((cp = bufferedReader.read()) != -1) {
                                sb.append((char) cp);
                            }
                            bufferedReader.close();
                        }    
                        in.close();
                        }
                        System.out.println("sb="+sb);
    
    0 讨论(0)
  • 2020-11-28 06:17

    In .Net you have the Response property of the WebException that gives access to the stream ON an exception. So i guess this is a good way for Java,...

    private InputStream dispatch(HttpURLConnection http) throws Exception {
        try {
            return http.getInputStream();
        } catch(Exception ex) {
            return http.getErrorStream();
        }
    }
    

    Or an implementation i used. (Might need changes for encoding or other things. Works in current environment.)

    private String dispatch(HttpURLConnection http) throws Exception {
        try {
            return readStream(http.getInputStream());
        } catch(Exception ex) {
            readAndThrowError(http);
            return null; // <- never gets here, previous statement throws an error
        }
    }
    
    private void readAndThrowError(HttpURLConnection http) throws Exception {
        if (http.getContentLengthLong() > 0 && http.getContentType().contains("application/json")) {
            String json = this.readStream(http.getErrorStream());
            Object oson = this.mapper.readValue(json, Object.class);
            json = this.mapper.writer().withDefaultPrettyPrinter().writeValueAsString(oson);
            throw new IllegalStateException(http.getResponseCode() + " " + http.getResponseMessage() + "\n" + json);
        } else {
            throw new IllegalStateException(http.getResponseCode() + " " + http.getResponseMessage());
        }
    }
    
    private String readStream(InputStream stream) throws Exception {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {
            String line;
            while ((line = in.readLine()) != null) {
                builder.append(line); // + "\r\n"(no need, json has no line breaks!)
            }
            in.close();
        }
        System.out.println("JSON: " + builder.toString());
        return builder.toString();
    }
    
    0 讨论(0)
提交回复
热议问题