5

I'm using in my android application the HttpURLConnection through a proxy where an authentication is needed. Here is my code and I will explain you after my problem.

HttpURLConnection connection = null;
int responseCode = -1;
try {
    connection = (HttpURLConnection) myUrl.openConnection();
    connection.setInstanceFollowRedirects(false);
    connection.setConnectTimeout(DEFAULT_TIMEOUT);
    connection.setReadTimeout(DEFAULT_TIMEOUT);

    responseCode = connection.getResponseCode();
    System.out.println("ResponseCode = " + responseCode);
} catch (IOException e) {
    System.out.println("Exception : " + e.getMessage());
}

My problem is that I get an exception on the getResponseCode() method which has the following message : Failed to authenticate with proxy. Usually, this specific error has an http error code : 407. But here I just got an exception but not a response code with the 407 value.

I have the solution to apply the login and password to connect to the proxy, but I want to apply this solution only in the case there is a 407 error (and not each time I catch an exception).

Any idea will be appreciated. Thanks.

1
  • did you find solution for this one? Answer from Tonio doesn't help. Commented Feb 13, 2018 at 16:06

2 Answers 2

0

Even though the question is pretty old I did not find a concrete answer so just for reference:

From my observations with Java 8, the getResponseCode in your example is the first method the actually request the server, that's why you'll get the IOException on this line.

Under the hood getInputStream is the trigger to actually establish the connection, however I would not recommend using it directly (as the implementation could change). Unfortunately calling connections.connect() does not work as expected and throws no Exception.

Finally connection.getResponseCode() also works after the exception has been thrown and this would be the key to get the response code even if the exception occured:

HttpURLConnection connection = null;
int responseCode = -1;
try {
    connection = (HttpURLConnection) myUrl.openConnection();
    connection.setInstanceFollowRedirects(false);
    connection.setConnectTimeout(DEFAULT_TIMEOUT);
    connection.setReadTimeout(DEFAULT_TIMEOUT);

    responseCode = connection.getResponseCode();
} catch (IOException e) {
    responseCode = connection.getResponseCode();
}
System.out.println("ResponseCode = " + responseCode);
Sign up to request clarification or add additional context in comments.

Comments

-1

Try catching HttpResponseException instead, and checking its status code (.getStatusCode()) to see if it's 407 (or better yet, HttpURLConnection.HTTP_PROXY_AUTH, more readable).

HttpResponseException is a subclass of IOException, and I think that's the actual exception that's being thrown.

(http://developer.android.com/reference/org/apache/http/client/HttpResponseException.html for more info.)

2 Comments

Why the downvote? Did the proposed solution not work? Is it no longer relevant (two-year-old answer, wouldn't be surprised!)?
I didn't downvote you, but I'm guessing whoever downvoted, did so because HttpResponseException is wrong. Looking at the source code for getResponseCode(), it calls getInputStream(), which in Android's HttpConnectionImpl.java, any HTTP response code >= 400 will throw FileNotFoundException. See source: android.googlesource.com/platform/libcore/+/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.