gpt4 book ai didi

java - 获取 javax.net.ssl.SSLException : Received fatal alert: protocol_version while scraping data using Jsoup

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:29 25 4
gpt4 key购买 nike

我正在尝试使用 Jsoup 从站点获取数据。该网站的链接是 Click here !

这是我获取数据的代码。`

    // WARNING: do it only if security isn't important, otherwise you have 
// to follow this advices: http://stackoverflow.com/a/7745706/1363265
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};

// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
;
}`


String url = "https://www.sos.nh.gov/corporate/soskb/SearchResults.asp?FormName=CorpNameSearch&Words=Starting&SearchStr="+query+"&SearchType=Search";
Connection.Response response = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.GET)
.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0")<br/>
.execute();
Document document = response.parse();

请告诉我这里的错误是什么。

最佳答案

您想在此处使用 Java 8,因为它默认支持 TLSv1.2 以及所需的额外密码套件。

为什么不是 Java 7?

我在我的机器上用 Java 7 (1.7.0_45) 测试了同样的错误。

我激活了调试消息并强制使用 TLSv1.2。

System.setProperty("javax.net.debug", "all");
System.setProperty("https.protocols", "TLSv1.2");

然后我遇到了这个新错误:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

最后,我去了Comodoca's SSL analyzer并看到一些有趣的东西。根据 SSL 分析器,您定位的站点仅启用了以下密码套件:

Cipher Suites EnabledName  (ID)                                       Key Size (in bits)TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256  (0xC02F)  128   ECDH 256-bit (P-256) TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384  (0xC030)  256   ECDH 256-bit (P-256) TLS_DHE_RSA_WITH_AES_128_GCM_SHA256  (0x9E)      128   DH 2048-bit  TLS_DHE_RSA_WITH_AES_256_GCM_SHA384  (0x9F)      256   DH 2048-bit

(see Full details)

On my side, I don't have any of the above suites. Check if you have them:

SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, null, new java.security.SecureRandom());

String[] scs = sc.getSocketFactory().getSupportedCipherSuites();
Arrays.sort(scs);

for(String s : scs) {
System.out.println(s);
}

参见 SSLSocketFactoryEx用于启用所需的密码套件。

为什么选择 Java 8?

另一方面,我通过从 Java 7 迁移到默认支持 TLS v1.2 并提供所需密码套件的 Java 8 (1.8.0_20) 成功地运行了代码。

这是 Windows 7 上 Java 8 (1.8.0_20) 支持的密码套件(总共 71 个套件)的精简列表。

TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
...
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256

片段

try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(java.security.cert.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());

// Fetch url
String url = "https://www.sos.nh.gov/corporate/soskb/SearchResults.asp?FormName=CorpNameSearch&Words=All&SearchStr=facebook&SearchType=Search";

Connection.Response response = Jsoup //
.connect(url) //
.timeout(60000) //
.method(Connection.Method.GET) //
.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0") //
.execute();

Document document = response.parse();
System.out.println(document);
} catch (Exception e) {
e.printStackTrace();
}

最后的想法:

When it comes to security, ALWAYS use the latest updated version.

关于java - 获取 javax.net.ssl.SSLException : Received fatal alert: protocol_version while scraping data using Jsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com