Remote Handshake exception occuring from service layer

148 Views Asked by At

When I am trying to execute this part of code

public String GetWebSite(String URL) {
        try {
            URL myUrl = new URL(URL);
            URLConnection connection = myUrl.openConnection();
            BufferedReader input = new BufferedReader(
                                    new InputStreamReader( connection.getInputStream() ));

            StringBuilder sb = new StringBuilder();

            String line;
            while ((line = input.readLine()) != null)
                sb.append(line); input.close();

            return sb.toString();
        } catch (Exception e) {
            return e.getMessage();
        }
    }

It's throwing this exception:

ssl peer shutdown incorrectly because of remote handshake exception
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946) ~[?:1.7.0_25]
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) ~[?:1.7.0_25]
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) ~[?:1.7.0_25]
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) ~[?:1.7.0_25]
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.3.5.jar:4.3.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) ~[httpclient-4.3.5.jar:4.3.5]
    at com.innodata.bsi.service.impl.CrossRefDoiService.uploadDoiToCrossRef(CrossRefDoiService.java:72) ~[CrossRefDoiService.class:?]
    at com.innodata.bsi.service.impl.CrossRefDoiService$$FastClassBySpringCGLIB$$31c97d98.invoke(<generated>) ~[spring-core-3.2.8.RELEASE.jar:?]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:700) ~[spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) ~[spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at com.innodata.bsi.interceptors.MethodLoggingAspect.logTimeMethod(MethodLoggingAspect.java:56) ~[MethodLoggingAspect.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_25]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) ~[spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:633) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at com.innodata.bsi.service.impl.CrossRefDoiService$$EnhancerBySpringCGLIB$$7cea208b.uploadDoiToCrossRef(<generated>) [spring-core-3.2.8.RELEASE.jar:?]
    at com.innodata.bsi.consumer.DDDoiRunConsumer.runDD(DDDoiRunConsumer.java:129) [DDDoiRunConsumer.class:?]
    at com.innodata.bsi.consumer.DDDoiRunConsumer.processRunDDMessage(DDDoiRunConsumer.java:98) [DDDoiRunConsumer.class:?]
    at com.innodata.bsi.consumer.DDDoiRunConsumer.handleMessage(DDDoiRunConsumer.java:72) [DDDoiRunConsumer.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_25]
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269) [spring-core-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:463) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:355) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:537) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:497) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:468) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1101) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1093) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:990) [spring-jms-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at java.lang.Thread.run(Thread.java:724) [?:1.7.0_25]
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:482) ~[?:1.7.0_25]
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927) ~[?:1.7.0_25]
    ... 55 more

This is the error while connecting a https web url :enter image description here

2

There are 2 best solutions below

2
piy26 On

Try using HttpsURLConnection connection = (HttpsURLConnection)myUrl.openConnection();

0
this_is_om_vm On

I have solved this problem by updating in java version 1.8.Could any one stillhelp me why the issue with java 1.7 ?How the same part of code is working with java 1.8?It's not recommendable action to solve out this issue please suggest me out.I have also tried to install trusted SSL certificate in my jvm keystore /jre/lib/security/cacerts .After installing certificate still the exception is not solve.