首页 文章

SSL对等在Java中关闭不正确

提问于
浏览
26

我需要通过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 的异常堆栈跟踪:

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工作正常 .

任何人都知道为什么会这样吗?

8 回答

  • 26

    这是安全协议的问题 . 我正在使用TLSv1,但主机只接受TLSv1.1和TLSv1.2然后我用下面的说明更改了Java中的协议:

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

  • 0

    您可以在系统属性中将协议版本设置为:

    overcome ssl handshake error

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

    除了公认的答案,其他问题也可能导致例外 . 对我而言,证书不受信任(即自签名证书,而不是信任存储区) .

    如果证书文件不存在,或者无法加载(例如,路径中的拼写错误),则在某些情况下---会导致相同的异常 .

  • 0

    我面临同样的问题,因为我向证券商店添加证书解决了这个问题 .

  • 2

    我有一个类似的问题,通过取消选中“高级安全性”中的“使用SSL 2.0兼容的ClientHello格式”选项来解决 .

  • -1

    此错误是安全库的通用错误,可能在其他情况下发生 . 如果其他人在使用javax.mail向smtp服务器发送电子邮件时遇到同样的错误 . 然后强制其他协议的代码是设置这样的属性:

    prop.put("mail.smtp.ssl.protocols", "TLSv1.2");            
    
    //And just in case probably you need to set these too
    prop.put("mail.smtp.starttls.enable", true);    
    prop.put("mail.smtp.ssl.trust", {YOURSERVERNAME});
    
  • 0

    接受的答案在我的情况下不起作用,不确定原因 . 我从JRE1.7切换到JRE1.8并自动解决了问题 . JRE1.8默认使用TLS1.2

  • 0

    就我而言,URL中只有空格 . 所以先查看查询字符串!

相关问题