0

我正在尝试使用 Java 客户端使用 REST 服务并获得java.net.ConnectException: Connection timed out异常。

如果我使用的是网络客户端(chrome、firefox),我可以检索到正确的答案。

我在 Windows 防火墙中添加了 java 的例外,但这并不能解决我的问题。我的连接是安全的。

SSLContext sslContext = SSLContext.getInstance("TLS");
        X509TrustManager[] xtmArray = new X509TrustManager[] { new OwnTrustManager() };
        sslContext.init(null, xtmArray, new java.security.SecureRandom());
        if (sslContext != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
                    .getSocketFactory());
        }
        HttpsURLConnection
                .setDefaultHostnameVerifier(new ISAHostnameVerifier());



public class OwnTrustManager implements X509TrustManager{
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

public X509Certificate[] getAcceptedIssuers() {
    return null;
}
}


public class ISAHostnameVerifier implements HostnameVerifier{
public boolean verify(String hostname, SSLSession session) {
    return true;
}
}

业务逻辑是:

URL url = null;
      try {

         url = new URL("https://16.9.1.82/xxxx/v23/rest");
         HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
         con.setRequestMethod("GET");
         con.setRequestProperty("Accept", "application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
         con.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
         con.setRequestProperty("Cookie", "----");
         con.setRequestProperty("Host", "16.9.1.82");
         con.setRequestProperty("Authorization", "Basic U1BQQ19STTFASUJNLkNPTTptYXhhZG1pbg==");
         con.connect();
4

0 回答 0