我正在尝试使用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部分但我的程序停止了。我不在乎解析特定的URL,我只是希望我的程序不会崩溃。有任何想法如何解决这个问题?
答案 0 :(得分:2)
由于在
初始化期间抛出了异常Document doc;
这一行
Elements links = doc.select("a[href]");
可能会引发一些您没有捕获的异常,或者返回的值将无法正确初始化