gpt4 book ai didi

java - 无法捕获 SSLException - Java

转载 作者:行者123 更新时间:2023-11-30 03:07:55 25 4
gpt4 key购买 nike

我试图用 Jsoup 解析一些 url,但出现此错误:

javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:512)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)

这是我的代码:

public Elements getLinks(String link){
Document doc = null;
try {
doc = Jsoup.connect(link)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.referrer("http://www.google.com")
.timeout(5000) //it's in milliseconds, so this means 5 seconds.
.get();
}
catch (SSLException e) {

e.printStackTrace();
}
catch (IOException e) {
if(e.getCause() instanceof SocketTimeoutException) {
try {
throw new SocketTimeoutException();
} catch (SocketTimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
e.printStackTrace();
}

Elements links = doc.select("a[href]");

return links;
}

我有一个 SSLException 的 catch 部分,但我的程序停止了。我不在乎解析具体的网址,我只是希望我的程序不要崩溃。有什么想法可以解决这个问题吗?

最佳答案

由于在初始化期间抛出异常

Document doc;

这一行

Elements links = doc.select("a[href]");

可能会抛出一些您未捕获的其他异常,或者返回的值将无法正确初始化

关于java - 无法捕获 SSLException - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298280/

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