HttpClient hangs while authenticating

258 Views Asked by At

I'm writing a Java program that connects to a web server (HTTPS and requires username/password) with this code, but it hangs when it reaches the execute call:

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("D:/jssecacerts"));
try {
    trustStore.load(instream, "certspassword".toCharArray());
} finally {
    try { instream.close(); } catch (Exception ignore) {}
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", 443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

HttpGet httpget = new HttpGet("https://server/path/default.aspx");

httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("server", 443),
    new UsernamePasswordCredentials("user", "pass"));

System.out.println("executing request" + httpget.getRequestLine());

HttpResponse response = httpclient.execute(httpget);

This is what I've tried in order to isolate the problem:

  • If I comment out the setCredentials line, it doesn't hang and the server instantly returns a HTTP/1.1 401 Unauthorized response
  • It doesn't matter if I provide a valid username/password or not
  • I've tried to specify timeouts with different methods but it remains hung after the timeout interval is over
0

There are 0 best solutions below