<分区>
标签 java ssl github-api
<分区>
我正在尝试开发一个 Java 程序来查询 GitHub GraphQL API。由于我是 Java 和 GitHub GraphQL API 的新手,我想我应该从一个简单的查询开始。一旦我开始工作,我就可以修改查询字符串,但我似乎无法让代码工作。你能帮我理解为什么这段代码不起作用以及我能做些什么来修复它吗?
非常感谢您的帮助。
我已经阅读了许多关于 Stack overflow 的问题,但我没有遇到我遇到的问题。我已经阅读了 Apache http 框架的描述,但他们的示例似乎比我想开发的更复杂。我发现了一些我无耻地复制的示例代码(见下文),这些代码看起来很简单,但我无法让它工作。
我在 macOS V10.14.6、Java SE 运行时环境(内部版本 1.7.0_51-b13)、Java HotSpot 64 位服务器虚拟机(内部版本 24.51-b03,混合模式)上运行,并使用 IntelliJ IDEA 2019.2 社区版(运行时版本 11.0.3+12-b304.10 x86_64)。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.json.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class httptestcode01
{
public static void main(String[] args) throws IOException
{
// Declare local variables...
String line, queryString, url;
url = "https://api.github.com/graphql";
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Authorization", "Bearer myToken");
httpPost.addHeader("Accept", "application/json");
JSONObject jsonobj = new JSONObject();
jsonobj.put("query", "{me { firstname } }");
try {
StringEntity entity = new StringEntity(jsonobj.toString());
httpPost.setEntity(entity);
response = client.execute(httpPost);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
while ((line = bufReader.readLine()) != null) {
builder.append(line);
builder.append(System.lineSeparator());
}
System.out.println(builder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我编译代码,运行它,得到以下输出。
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/tools.jar:/Volumes/GoogleDrive/My Drive/soci code base/http test code 01/target/classes:/Users/ciro/.m2/repository/org/apache/httpcomponents/httpclient/4.3.4/httpclient-4.3.4.jar:/Users/ciro/.m2/repository/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar:/Users/ciro/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/ciro/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/Users/ciro/.m2/repository/org/json/json/20190722/json-20190722.jar" httptestcode01
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:1959)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at httptestcode01.main(httptestcode01.java:36)
Process finished with exit code 0
最佳答案
虽然“javax.net.ssl.SSLException: Received fatal alert: protocol_version”解释了 Java 1.6 和 1.7 默认已过时的 TLS1.0,但在 https://api.github.com
的上下文中并没有这样做。 .
Since February 2017 , GitHub 已停止支持弱加密标准,包括 TLSv1。
如 this bug report 中所述:
- Oracle JDK 1.7u80 is the last Oracle public release
- If you used 1.7u131 (from Oracle paying support or OpenJDK), you would not need the system property: see release notes:
"Add TLS v1.1 and v1.2 to the client list of default-enabled protocols"
在您的情况下,至少尝试添加到您的 java 命令中:
-Dhttps.protocols=TLSv1.2
或者升级到更新的 Java 版本,特别是考虑到 jdk 1.7 是 eol -- end of life -- since May 2015 .
即使通过商业支持,根据 Java roadmap , Premier support已于上个月(2019 年 7 月)结束,延长支持将于 2022 年 7 月结束。
关于java - 如何从 Java 进行 GitHub GraphQL API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532309/
相关文章:
Angular 默认应用服务:Chrome 中的隐私错误:NET::ERR_CERT_AUTHORITY_INVALID
github - 检索 GitHub 用户的成员(member)信息
javascript - 使用 Github API 获取版本的所有提交部分
java - 在 java 中使用 parse4j 向设备发送推送通知
shell - 错误 : site default-ssl not properly enabled
javascript - 如何从 GitHub 获取 API 数组的所有 'items'?
java - IllegalStateException [start 在无效状态下调用 : 1] on restarting Android MediaRecorder