简体   繁体   English

连接超时到HTTPS URL

[英]Connection timeouts to HTTPS URLs

I'm needing to ignore all SSL certificates in Java, but I can't for the life of me get it to work. 我需要忽略Java中的所有SSL证书,但我不能让我的生活让它工作。 I've looked through the following pages listed below already, but nothing seems to work on every https link. 我已经查看了下面列出的以下页面,但似乎没有任何内容可用于每个https链接。

stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3
stackoverflow.com/questions/13470998/ignoring-ssl-validation-in-java
stackoverflow.com/questions/12060250/ignore-ssl-certificate-errors-with-java
stackoverflow.com/questions/2694281/ignore-certificate-errors-when-requesting-a-url-in-java
stackoverflow.com/questions/6681969/java-ignore-certificate-validation
www.nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/
code.google.com/p/misc-utils/wiki/JavaHttpsUrl
www.exampledepot.8waytrips.com/egs/javax.net.ssl/TrustAll.html
www.obsidianscheduler.com/blog/ignoring-self-signed-certificates-in-java/
java.dzone.com/articles/how-ignore-cert-and-host-name
gist.github.com/henrik242/1510165

I have a good reason for needing to do this so don't worry, but I really need to be able to do it. 我有充分的理由需要这样做所以不要担心,但我真的需要能够做到这一点。 Basically, I'm needing to go through a list of internal https links and check to make sure that they are all still valid and aren't broken links. 基本上,我需要浏览一个内部https链接列表并检查以确保它们仍然有效并且没有损坏的链接。 Some links works fine since the Java code ignores the certificate and can get an HTTP response header back, but others just timeout even though they work fine in my web browser. 一些链接工作正常,因为Java代码忽略了证书并且可以返回HTTP响应头,但是其他链接只是超时,即使它们在我的Web浏览器中正常工作。 All of these links are internal company links. 所有这些链接都是公司内部链接。

I've tried using HttpsURLConnection as well as HttpGet and HttpClient. 我尝试过使用HttpsURLConnection以及HttpGet和HttpClient。 Could there be something else that I'm not thinking of, or something unrelated to Java that could be causing the pages to timeout? 可能还有其他一些我没想到的东西,或者与Java无关的东西可能导致页面超时? I just want to make sure the URL of the link exists. 我只是想确保链接的URL存在。 Here are the exceptions I am getting. 以下是我得到的例外情况。

With HttpGet/SSLContextBuilder/PoolingHttpClientConnectionManager: 使用HttpGet / SSLContextBuilder / PoolingHttpClientConnectionManager:

org.apache.http.conn.HttpHostConnectException: Connect to -removed- [-removed-] failed: Connection timed out: connect

With HttpsUrlConnection using X509TrustManager: 使用X509TrustManager的HttpsUrlConnection:

java.net.ConnectException: Connection timed out: connect

Specifically, I've tried the following and many variations of it based on the links posted above: 具体来说,我根据上面发布的链接尝试了以下和它的许多变化:

TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
};

// Install the all-trusting trust manager
javax.net.ssl.SSLContext sc = null;

try {
    sc = javax.net.ssl.SSLContext.getInstance("TLS");
    sc.init(null, trustAllCerts, new SecureRandom());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

I've also tried this as well as several variations: https://stackoverflow.com/a/19950935/1727920 我也试过这个以及几个变种: https//stackoverflow.com/a/19950935/1727920

Connection timeouts have nothing whatsoever to do with SSL certificates. 连接超时与SSL证书无关。

More likely you don't have the same HTTP Proxy settings as the browser. 您更可能没有与浏览器相同的HTTP代理设置。 You need to set the system properties http.proxyHost and http.proxyPort to the same values used by the browser. 您需要将系统属性http.proxyHosthttp.proxyPort设置为浏览器使用的相同值。 If the HTTPS proxy settings are different from the HTTP proxy settings, set https.proxyHost and https.proxyPort accordingly. 如果HTTPS代理设置与HTTP代理设置不同,请相应地设置https.proxyHosthttps.proxyPort

EDIT For completeness: A lot of old sources erroneously mention a proxySet property. 编辑为了完整性:许多旧的源错误地提到了proxySet属性。 There is not and has never been such a property in the JDK. JDK中没有也从未有过这样的属性。 It was in the short-lived and long-defunct HotJava Bean of 1997. Similarly http.proxySet doesn't exist either. 它是在1997年的短命和长期停止的HotJava Bean中。类似地, http.proxySet也不存在。 Proof: try setting them to false in circumstances where they should be true, and watch your program keep working. 证明:尝试在它们应该是true,情况下将它们设置为false true,并观察您的程序是否继续工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM