从HttpURLConnection获取数据作为文本抛出SourceForge.net的javax.net.ssl.SSLHandshakeException [英] Fetching data from HttpURLConnection as text throws javax.net.ssl.SSLHandshakeException for SourceForge.net
问题描述
Java Version 1.8.0_121,我只看到了Java 9的解决方案
Java Version 1.8.0_121 , I see a solution for Java 9 only
描述:
Description:
我的目标是获取有关 SourceForge
项目的信息,例如项目的总下载量。
SourceForge
有一个名为 SourceForge下载统计API 。
My goal is to get informations about SourceForge
Projects , such as the total downloads of a Project .
SourceForge
has an API for that called SourceForge Downloads Stats API .
所以我创建了一个简单的程序,将结果视为原始文本,我没有必要将它作为JSON。
So i created a simple program see the result as raw text , i don't necessary need it as JSON .
例如我获取有关 Apache的信息OpenOffice 下载等等 - > 链接,您可以尝试查看
For example i am gettings information about Apache OpenOffice downloads and many more -> Link , you can try it to see
但我无法使用 Java
以下错误原因。我已经测试了其他网站的代码并且运行良好,但对于这个我不知道原因。
But i can't fetch the data using Java
cause of the error below . I have tested the code with other websites and it works well , but for this one i don't know why .
Java代码:
Java Code:
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();
}
}
错误:
Error:
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)
最后
上述解决方案将不胜感激。
Finally
A solution on the above will be greatly appreciated .
尝试使用Andy的答案会产生同样的错误。
Trying using Andy's answer produces the same error.
推荐答案
以下仅用于获取我实际需要的下载量。所以这不是完整的答案。
The below is only for getting the downloads which i actually needed. So this is not the complete answer.
Here is a tricky way to get the Total downloads using the svg image that SourceForge is providing for downloads :
要获得每个
周/日/年
的下载,您只需更改一些
位代码即可删除不合适的文本。
To get the downloads per
week/day/year
you can just change a little bit the code to remove the innapropriate text.
检查从 BufferedReaded
返回的文本,就像我为
总下载量所做的那样。
Check the text that is returned from the BufferedReaded
as i did for
total downloads.
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]);
}
}
这篇关于从HttpURLConnection获取数据作为文本抛出SourceForge.net的javax.net.ssl.SSLHandshakeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!