HttpClient在进行身份验证时挂起

时间:2013-09-05 10:42:22

标签: java http apache-httpclient-4.x http-authentication

我正在编写一个使用此代码连接到Web服务器(HTTPS并需要用户名/密码)的Java程序,但它在到达execute调用时挂起:

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);

这是我为了隔离问题而尝试过的:

  • 如果我注释掉setCredentials行,它就不会挂起,服务器会立即返回HTTP/1.1 401 Unauthorized响应
  • 如果我提供有效的用户名/密码
  • 则无关紧要
  • 我尝试使用不同的方法指定超时,但在超时间隔结束后仍然挂起

0 个答案:

没有答案