2015-03-06 13 views
12

Tôi cần thực hiện yêu cầu thông qua giao thức HTTPS. Tôi đã viết đoạn mã sau:Đồng đẳng SSL tắt không chính xác trong Java

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(); 
     } 
    } 

} 

tôi đã stacktrace ngoại lệ này với một 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 

tôi có phản ứng thành công từ https://google.com nhưng lỗi này từ URL ở trên (https://ancine.band.com.br/xml/pgrt1_dta_20150303.xml).

Sử dụng PHP, .NET và NodeJS mà URL hoạt động tốt.

Bất kỳ ai cũng biết tại sao điều này lại xảy ra?

+0

phiên bản java bạn đang sử dụng để chạy chương trình? –

+3

Hãy chạy ứng dụng khách của bạn với '-Djavax.net.debug = ssl, handshake' và đăng kết quả đầu ra trong câu hỏi của bạn. – EJP

+0

Tôi đang sử dụng ** Môi trường chạy OpenJDK (IcedTea 2.5.4) (7u75-2.5.4-1 ~ trusty1) **. – aneto

Trả lời

18

Đó là vấn đề của giao thức bảo mật. Tôi đang sử dụng TLSv1 nhưng máy chủ chỉ chấp nhận TLSv1.1 và TLSv1.2 sau đó tôi đã thay đổi giao thức trong Java với hướng dẫn dưới đây:

;

+2

Làm thế nào để bạn biết phiên bản giao thức bảo mật nào được máy chủ chấp nhận? – testerjoe2

+0

Ngoài ra, câu trả lời này không hiệu quả đối với tôi. – testerjoe2

2

Ngoài câu trả lời được chấp nhận, các vấn đề khác cũng có thể gây ra ngoại lệ. Đối với tôi, chứng chỉ không đáng tin cậy (tức là, chứng chỉ tự ký và không phải trong kho ủy thác).

Nếu tệp chứng chỉ không tồn tại hoặc không thể tải (ví dụ: lỗi đánh máy trong đường dẫn) có thể --- trong một số trường hợp nhất định --- gây ra cùng một ngoại lệ.

+1

Nếu đó là chứng chỉ tự ký, cách giải quyết lỗi này? –

+0

Thêm nó vào cửa hàng tin cậy của bạn (với toàn bộ hệ điều hành hoặc xác định một khi khởi động JVM bằng '-Djavax.net.ssl.trustStore = ...' và '-Djavax.net.ssl.trustStorePassword = ... '.) –

-1

Trong trường hợp của tôi chỉ có khoảng trắng trong URL. Vì vậy, hãy kiểm tra chuỗi truy vấn trước tiên!

+0

Điều đó sẽ gây ra một lỗi khác. Nó sẽ không ngăn cản việc bắt tay hoàn thành. – EJP

0

Bạn có thể đặt các phiên bản giao thức trong hệ thống tài sản như:

khắc phục lỗi bắt tay ssl

System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); 
Các vấn đề liên quan