NLemay NLemay - 1 year ago 255
Android Question

HttpUrlConnection doesn't find the NTLM challenge on Android

I'm trying to connect my Android app to an IIS server using the HttpUrlConnection class.

My server needs the user to be authenticate, so it is sending the following challenge to the client :

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

My problem is that HttpUrlConnection doesn't seems to parse it. So getPasswordAuthentication() is never call, and it return an IOException "no authentication challenges found".

Here is my code :

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("myUsername", "myPassword".toCharArray());

URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestProperty("Accept-Encoding", "gzip");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "close");

status_code = conn.getResponseCode();
}catch (IOException e) {

I'm really starting to think that NTLM challenge is just not supported by HttpUrlConnection. I saw some libraries that seems to do the work, but I would prefer not to use external libraries.

Can somebody confirm if it is possible or not to make HttpUrlConnection handle NTLM challenge without external libs?

Answer Source

I've only been able to make it work with HttpClient by setting the AuthScheme and the library below:

HttpClient httpclient = new HttpClient(httpParameters, context);
NTCredentials creds = new NTCredentials(“username”, “password”, "", "dir");
              new AuthScope(context.getString(“whatever is your main URL”), -1), creds);
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());

Then you implement the the JCIFS engine and factory. You can find samples in

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download