1

我正在尝试使用 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 的捕获部分,但我的程序停止了。我不在乎解析特定的 url,我只想让我的程序不要崩溃。任何想法如何解决这个问题?

4

1 回答 1

2

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

Document doc;

这条线

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

可能会抛出一些您没有捕获的其他异常,或者返回的值不会被正确初始化

于 2015-12-15T20:07:18.560 回答