我正在编写一个 Java 程序,该程序使用此代码连接到 Web 服务器(HTTPS 并且需要用户名/密码),但是当它到达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
响应 - 我是否提供有效的用户名/密码并不重要
- 我尝试使用不同的方法指定超时,但超时间隔结束后它仍然挂起