Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HttpURLConnection.getInputStream but get 401 IOException

I am writing a REST client for CouchDB in Java. The following code should be quite standard:

    this.httpCnt.connect();
    Map<String, String> responseHeaders = new HashMap<>();
    int i = 1;
    while (true){
        String headerKey = this.httpCnt.getHeaderFieldKey(i);
        if (headerKey == null)
            break;
        responseHeaders.put(headerKey, this.httpCnt.getHeaderField(i));
        i++;
    }
    InputStreamReader reader = new InputStreamReader(this.httpCnt.getInputStream());
    StringBuilder responseBuilder = new StringBuilder();
    char[] buffer = new char[1024];
    while(true){
        int noCharRead = reader.read(buffer);
        if (noCharRead == -1){
            reader.close();
            break;
        }
        responseBuilder.append(buffer, 0, noCharRead);
    }

I want to test what happen if the authentication fails. However if the authentication fails, when calling getInputStream of the HttpURLConnection, I get directly an IOException saying the server responses 401. I suppose if the server responses something, no matter success or failure, it should be able to read whatever the server returns. And I am sure in this case the server does return some text in the body, since if I do a GET to the server using curl and the authentication fails, I get a JSON object as the response body with some error messages in it.

Is there any way to still get the response body even if 401?

like image 795
Weixiang Guan Avatar asked May 11 '14 13:05

Weixiang Guan


People also ask

How do I turn off HttpURLConnection in Java?

To close the connection, invoke the close() method on either the InputStream or OutputStream object. Doing that may free the network resources associated with the URLConnection instance.

How do I get HttpURLConnection response code?

openConnection(); connection. setRequestMethod("GET"); connection. connect(); int code = connection. getResponseCode();

What is HttpURLConnection in Java?

public abstract class HttpURLConnection extends URLConnection. A URLConnection with support for HTTP-specific features. See the spec for details. Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.

When should I use HttpURLConnection?

The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance. It sets whether HTTP redirects (requests with response code) should be automatically followed by HttpURLConnection class.


2 Answers

You need to check for the http status using getResponseCode() to decide if you should use getInputStream() or getErrorStream(). In this case, you need to read the error stream.

like image 152
Zavior Avatar answered Oct 18 '22 18:10

Zavior


See this question:

"The HttpURLConnection.getErrorStream method will return an InputStream which can be used to retrieve data from error conditions (such as a 404), according to the javadocs."

like image 40
user3001 Avatar answered Oct 18 '22 20:10

user3001