我們試着寫一個REST客戶端與HTTPS和基本認證。代碼如下。我得到以下異常:新澤西州的REST ClientHandlerException:java.net.ConnectException:連接超時:連接
java.net.ConnectException:連接超時:連接
任何一個可以幫助我找到了我做錯了嗎?
public class sampleMain {
public static void main(String[] args) {
ClientConfig clientConfig = new DefaultClientConfig();
/* clientConfig.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);*/
clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, 300000);
clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 30000);
disableSslVerification(clientConfig);
Client client = Client.create(clientConfig);
client.setFollowRedirects(true);
HTTPBasicAuthFilter authenticationFilter = new HTTPBasicAuthFilter("admin", "[email protected]#1234");
client.addFilter(authenticationFilter);
WebResource webResource = client.resource("https://someIP:443/rest/api/topology/servicesnodes");
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
System.out.println(response);
}
//below method is used to set ssl context to clientconfig as https property
private static void disableSslVerification(ClientConfig clientConfig) {
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}, sc));
} catch (NoSuchAlgorithmException ae1) {
} catch (KeyManagementException e) {
}
}
}
我得到異常下面我:java.net.ConnectException:連接超時:連接 \t在java.net.DualStackPlainSocketImpl。 waitForConnect(本機方法) \t在java.net.DualStackPlainSocketImpl.socketConnect(來源不明) \t在java.net.AbstractPlainSocketImpl.doConnect(來源不明) \t在java.net.AbstractPlainSocketImpl.connectToA ddress(未知來源) \t在java.net.AbstractPlainSocketImpl.connect(未知來源) \t在java.net.PlainSocketImpl.connect(未知來源) \t在java.net.SocksSocketImpl.connect(未知來源) \t在java.net.Socket.connect(未知來源) \t at sun.security.ssl.SSL – Rockzz
哪裏出現異常?如果你可以打印它堆棧跟蹤並將其添加到問題 – user7294900