当前位置: 首页 > 面试题库 >

SSL对等端在Java中错误地关闭

陈毅
2023-03-14
问题内容

我需要通过HTTPS协议发出请求。我写了以下代码:

import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;

public class XMLHandlerTest {
    private static final String URL = "https://ancine.band.com.br/xml/pgrt1_dta_20150303.xml";

    @Test
    public void testRetrieveSchedule() {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(URL).openConnection();
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            System.out.println(responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

我使用 java.io.EOFException 得到了这个异常stacktrace :

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:953)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at br.com.onebr.onesocial.arte1.service.core.scheduler.Arte1XMLHandlerTest.testRetrieveSchedule(Arte1XMLHandlerTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:482)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
    ... 32 more

我从 https://google.com
获得了成功的响应,但是以上URL(https://ancine.band.com.br/xml/pgrt1_dta_20150303.xml)中的此错误。

使用PHP,.NET和NodeJS,URL可以正常工作。

有人知道为什么会这样吗?


问题答案:

那是安全协议的问题。我正在使用TLSv1,但是主机仅接受TLSv1.1和TLSv1.2,然后我按照以下说明更改了Java中的协议:

System.setProperty("https.protocols", "TLSv1.1");



 类似资料:
  • 我有一个问题,集成liquibase maven执行与一个mysql 5.7数据库由Azure作为一个服务.错误日志是: 无法执行目标组织。liquibase:liquibase maven插件:3.0.5:项目上的状态(默认cli)-:设置或运行liquibase:com时出错。mysql。jdbc。例外情况。jdbc4。通信异常:通信链路故障发送到服务器的最后一个数据包是575毫秒前的。握手期

  • 我需要通过HTTPS协议提出请求。我编写了以下代码: 有人知道为什么会这样吗?

  • 我有以下代码: 当我运行代码时,我得到以下异常消息:期间与远程主机的连接失败。 我不知道为什么我会收到这条信息。

  • 如果我在以下URL上运行上述代码:https://eztv.it/shows/887/the-blacklist/,它将按预期工作。两个URL文件大小之间的差异似乎是一个促成因素。在测试同一个服务器的不同URL时,上面的代码似乎只适用于小于30KB的文件。以上任何内容都将生成上述异常。

  • 在jenkins中调试从nexus下载的maven后,我出现了以下错误。 Openssl工作正常,证书是由Amazon发布的有效证书。

  • 问题内容: 我正在尝试做一个简单的HttpGet来读取网页。我在iOS和Android上通过http(而非https)工作。 网址是内部网络IP和自定义端口,因此我可以使用以下路径使用http读取此类内容: 当我尝试使用https时,出现错误。所以我尝试使用此代码: 但这给我一个错误。 我究竟做错了什么?我可以放心地忽略该证书,因为它是一个具有自签名证书的内部网络,但是我无法控制版本,我的应用程序