Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


Content-Length
HTTP/1.1 401 Unauthorized
HttpURLConnection.getResponseCode()
WWW-Authenticate
java.io.IOException: Received authentication challenge is null
try {
    response=connection.getResponseCode();
}
catch (java.io.IOException e) {
    if (e.getMessage().contains("authentication challenge")) {
        response = HttpsURLConnection.HTTP_UNAUTHORIZED;
    } else { throw e; }
}

If you can't make changes to server, then you can catch that exception (thanks http://stackoverflow.com/a/10904318/262462)

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}
  • Add a fake "WWW-Authenticate" header like: WWW-Authenticate: Basic realm="fake". This is a mere workaround not a solution, but it should work and the http client is satisfied.
  • Use HTTP status code 403 instead of 401. It's semantic is not the same and usually when working with login 401 is a correct response (see here for detailed discussion) but its close enough.

As @ErikZ wrote in his post you could use a try&catch

Possible solutions if you can change the server:

Possible solutions if you can't change the server:

This error happens beause the server sends a 401 (Unauthorized) but does not give a "WWW-Authenticate" which is a hint for the client what to do next. The "WWW-Authenticate" Header tells the client which kind of authentication is needed (either Basic or Digest). This is usually not very useful in headless http clients, but thats how the standard is defined. The error occurs because the lib tries to parse the "WWW-Authenticate" header but can't.

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}

You can get the response code after an exception if you call .getResponseCode() a second time on the connection object. This is because the first time you call .getResponseCode() an internal state is set that enables .getResponseCode() to return without throwing an exception.

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}

You can get the response code after an exception if you call .getResponseCode() a second time on the connection object. This is because the first time you call .getResponseCode() an internal state is set that enables .getResponseCode() to return without throwing an exception.

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}

You can get the response code after an exception if you call .getResponseCode() a second time on the connection object. This is because the first time you call .getResponseCode() an internal state is set that enables .getResponseCode() to return without throwing an exception.

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


Content-Length
HTTP/1.1 401 Unauthorized
HttpURLConnection.getResponseCode()
WWW-Authenticate
java.io.IOException: Received authentication challenge is null
try {
    response=connection.getResponseCode();
}
catch (java.io.IOException e) {
    if (e.getMessage().contains("authentication challenge")) {
        response = HttpsURLConnection.HTTP_UNAUTHORIZED;
    } else { throw e; }
}

If you can't make changes to server, then you can catch that exception (thanks https://stackoverflow.com/a/10904318/262462)

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


Content-Length
HTTP/1.1 401 Unauthorized
HttpURLConnection.getResponseCode()
WWW-Authenticate
java.io.IOException: Received authentication challenge is null
try {
    response=connection.getResponseCode();
}
catch (java.io.IOException e) {
    if (e.getMessage().contains("authentication challenge")) {
        response = HttpsURLConnection.HTTP_UNAUTHORIZED;
    } else { throw e; }
}

If you can't make changes to server, then you can catch that exception (thanks https://stackoverflow.com/a/10904318/262462)

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}
  • Add a fake "WWW-Authenticate" header like: WWW-Authenticate: Basic realm="fake". This is a mere workaround not a solution, but it should work and the http client is satisfied.
  • Use HTTP status code 403 instead of 401. It's semantic is not the same and usually when working with login 401 is a correct response (see here for detailed discussion) but its close enough.

As @ErikZ wrote in his post you could use a try&catch

Possible solutions if you can change the server:

Possible solutions if you can't change the server:

This error happens beause the server sends a 401 (Unauthorized) but does not give a "WWW-Authenticate" which is a hint for the client what to do next. The "WWW-Authenticate" Header tells the client which kind of authentication is needed (either Basic or Digest). This is usually not very useful in headless http clients, but thats how the standard is defined. The error occurs because the lib tries to parse the "WWW-Authenticate" header but can't.

Note
Rectangle 27 1

android java.io.IOException: Received authentication challenge is null in ICS 4.0.3?


HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}
  • Add a fake "WWW-Authenticate" header like: WWW-Authenticate: Basic realm="fake". This is a mere workaround not a solution, but it should work and the http client is satisfied.
  • Use HTTP status code 403 instead of 401. It's semantic is not the same and usually when working with login 401 is a correct response (see here for detailed discussion) but its close enough.

As @ErikZ wrote in his post you could use a try&catch

Possible solutions if you can change the server:

Possible solutions if you can't change the server:

This error happens beause the server sends a 401 (Unauthorized) but does not give a "WWW-Authenticate" which is a hint for the client what to do next. The "WWW-Authenticate" Header tells the client which kind of authentication is needed (either Basic or Digest). This is usually not very useful in headless http clients, but thats how the standard is defined. The error occurs because the lib tries to parse the "WWW-Authenticate" header but can't.

Note