[英]Connection timeouts to HTTPS URLs
我需要忽略Java中的所有SSL證書,但我不能讓我的生活讓它工作。 我已經查看了下面列出的以下頁面,但似乎沒有任何內容可用於每個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
我有充分的理由需要這樣做所以不要擔心,但我真的需要能夠做到這一點。 基本上,我需要瀏覽一個內部https鏈接列表並檢查以確保它們仍然有效並且沒有損壞的鏈接。 一些鏈接工作正常,因為Java代碼忽略了證書並且可以返回HTTP響應頭,但是其他鏈接只是超時,即使它們在我的Web瀏覽器中正常工作。 所有這些鏈接都是公司內部鏈接。
我嘗試過使用HttpsURLConnection以及HttpGet和HttpClient。 可能還有其他一些我沒想到的東西,或者與Java無關的東西可能導致頁面超時? 我只是想確保鏈接的URL存在。 以下是我得到的例外情況。
使用HttpGet / SSLContextBuilder / PoolingHttpClientConnectionManager:
org.apache.http.conn.HttpHostConnectException: Connect to -removed- [-removed-] failed: Connection timed out: connect
使用X509TrustManager的HttpsUrlConnection:
java.net.ConnectException: Connection timed out: connect
具體來說,我根據上面發布的鏈接嘗試了以下和它的許多變化:
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);
}
我也試過這個以及幾個變種: https : //stackoverflow.com/a/19950935/1727920
連接超時與SSL證書無關。
您更可能沒有與瀏覽器相同的HTTP代理設置。 您需要將系統屬性http.proxyHost
和http.proxyPort
設置為瀏覽器使用的相同值。 如果HTTPS代理設置與HTTP代理設置不同,請相應地設置https.proxyHost
和https.proxyPort
。
編輯為了完整性:許多舊的源錯誤地提到了proxySet
屬性。 JDK中沒有也從未有過這樣的屬性。 它是在1997年的短命和長期停止的HotJava Bean中。類似地, http.proxySet
也不存在。 證明:嘗試在它們應該是true,
情況下將它們設置為false
true,
並觀察您的程序是否繼續工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.