异常:javax.net.ssl.SSLHandshakeException: Remote host closed connection during han

public class NewClient extends DefaultHttpClient{
	public NewClient() throws Exception{
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
	
	public void get(){
		 try {
	            //用get方法发送http请求
	            HttpGet get = new HttpGet("https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck");
	            System.out.println("执行get请求:...."+get.getURI());
	            CloseableHttpResponse httpResponse = null;
	            //发送get请求
	            httpResponse = this.execute(get);
	            try{
	                //response实体
	                HttpEntity entity = httpResponse.getEntity();
	                if (null != entity){
	                    System.out.println("响应状态码:"+ httpResponse.getStatusLine());
	                    System.out.println("-------------------------------------------------");
	                    System.out.println("响应内容:" + EntityUtils.toString(entity));
	                    System.out.println("-------------------------------------------------");                    
	                }
	            }
	            finally{
	                httpResponse.close();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	public static void main(String[] args) {
		NewClient client;
		try {
			client = new NewClient();
			client.get();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

异常:

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)

at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:553)

at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:412)

at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179)

at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:328)

at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612)

at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447)

at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

at Tool.NewClient.get(NewClient.java:103)

at Tool.NewClient.main(NewClient.java:126)

Caused by: java.io.EOFException: SSL peer shut down incorrectly

at sun.security.ssl.InputRecord.read(Unknown Source)

... 15 more

解决方案:把jdk8替换成jdk7

猜你喜欢

转载自ydz00ydz.iteye.com/blog/2326584