This question already has answers here:
javax.net.ssl.SSLException: Received fatal alert: protocol_version
                                
                                    (9个答案)
                                
                        
                                4年前关闭。
            
                    
我正在尝试使用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,我只是希望我的程序不崩溃。任何想法如何解决此问题?

最佳答案

作为例外,在初始化过程中会抛出蜜蜂

Document doc;


这条线

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


可能会引发其他一些您没有捕获的异常,否则返回值将无法正确初始化

10-02 09:02