4

I am trying to authenticate against a server supporting NTLM authentication in my Java application using java.net's HttpUrlConnection.

The problem is that the application fails to send the Authenticate response.

I have attached proxy to the http connection so that the traffic is routed via fiddler. In fiddler i can see Negotiate message being sent, and server Challenge is received. But after this, the application just stops saying it got a 401, without sending the type3 authenticate message.

Any ideas? Here is the code -

Authenticator.setDefault(new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("username",
                                          "password".toCharArray());
    }
});
URL url = new URL("https://service_url.svc");

// Proxy for fiddler
Proxy proxy = new Proxy(Proxy.Type.HTTP,
                  new InetSocketAddress("localhost", 8888));

// Create a connection
HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);

InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null)
    System.out.println(line);

rd.close();

Appreciate any help. Thanks.

Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • No experience here? Some one must have tried this for sure! – Bonton255 Apr 28 '12 at 05:52
  • I have the exact same problem. With NTLM challenge returned by the server, getPasswordAuthentication() is never call. Did you find a solution? – NLemay Jun 18 '13 at 15:21
  • 1
    Yes i solved this problem, but it was looong back so i dont know the solution. The way i figured was by stepping into the apache http client code. – Bonton255 Jul 26 '13 at 06:44
  • An example using HttpURLConnection : http://stackoverflow.com/a/34321230/2073804 – ron190 Dec 20 '15 at 02:44

0 Answers0