从HttpURLConnection获取数据作为文本抛出SourceForge.net的javax.net.ssl.SSLHandshakeException

时间:2017-05-17 17:30:03

标签: java json ssl

Java版本1.8.0_121,I see a solution for Java 9 only

  

说明

我的目标是获取有关SourceForge项目的信息,例如项目的总下载量。 SourceForge有一个名为SourceForge Downloads Stats API的API。

所以我创建了一个简单的程序,将结果看作原始文本,我不需要它作为JSON。

例如,我获取有关Apache OpenOffice次下载的信息以及更多信息 - > Link , you can try it to see

但我无法使用以下错误导致Java原因获取数据。我已经在其他网站上对代码进行了测试,效果很好,但对于这个我不知道为什么。

  

Java代码:

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        //Create HttpURLConnection 
        HttpURLConnection httpcon = (HttpURLConnection) new URL(
                "https://sourceforge.net/projects/openofficeorg.mirror/files/stats/json?start_date=2014-10-29&end_date=2014-11-04").openConnection();
        //In case it might to the trick...
        httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
        //Read the inputstream
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));

        //Print everything
        String inputLine;
        while ( ( inputLine = in.readLine() ) != null)
            System.out.println(inputLine);
        in.close();
    }
}
  

错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal a
lert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    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:13
75)
    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(Abstra
ctDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnectio
n.java:1546)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection
.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLCon
nectionImpl.java:254)
    at aaTester.URLReader.main(URLReader.java:13)

最后

上述解决方案将不胜感激。

尝试使用Andy的答案会产生同样的错误。

3 个答案:

答案 0 :(得分:1)

comment从@ GOXR3PLUS转换为答案:

升级到至少Java 8u161(或Java 9)会使SSLHandshakeException消失,从SourceForge下载。

答案 1 :(得分:0)

您不能要求最终用户安装策略,但是有一些编程方式可以实施策略。 免责声明:不建议这样做,可能会产生TOS和ITAR影响。

在TLS协商之前在static块中执行的代码应该允许JVM使用AES-256密码套件。

if (Cipher.getMaxAllowedKeyLength("AES") < 256) {
  try {
    Field field = Class.forName("javax.crypto.JceSecurity").
    getDeclaredField("isRestricted");
    field.setAccessible(true);
    field.set(null, java.lang.Boolean.FALSE);
  } catch (Exception e) {
    fail("Could not override JCE cryptography strength policy setting");
    fail(e.getMessage());
  }
}

答案 2 :(得分:0)

以下仅用于获取我实际需要的下载量。所以这不是完整的答案。

使用SourceForge为下载提供的svg image获取总下载次数是一种棘手的方法:

  

要获得week/day/year的下载量,您可以稍微更改一下   咬掉代码以删除不合适的文本。

     

检查我所做的BufferedReaded返回的文字   总下载量。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;


public class URLReader {
    public static void main(String[] args) throws Exception {

        //System.out.println(Cipher.getMaxAllowedKeyLength("AES"));

        //Create HttpURLConnection  
        HttpURLConnection httpcon = (HttpURLConnection) new URL("https://img.shields.io/sourceforge/dt/xr3player.svg").openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));

        //Read line by line
        String line = "" , inputLine;
        while ( ( inputLine = in.readLine() ) != null) {
            line += "\n" + inputLine;
            System.out.println(inputLine);
        }
        in.close();

        //Get SourceForge Downloads 
        System.out.println("Total Downlads: "+line.split("<text x=\"98.5\" y=\"14\">")[1].split("/total")[0]);
    }

}