Getting http 407 error as an IOException

I use in my android application HttpURLConnectionthrough a proxy server where authentication is required. Here is my code and I will explain to 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 in the method getResponseCode (), which has the following message: Failed to authenticate with proxy. Usually this error has an error code of http: 407. But here I received only an exception, but not a response code with a value of 407.

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

Any idea would be appreciated. Thanks.

+4
1

HttpResponseException (.getStatusCode()), , 407 ( , HttpURLConnection.HTTP_PROXY_AUTH, ).

HttpResponseException IOException, , , .

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

-1

All Articles