jdk1.7访问https出现握手失败的情况,原因是jdk1.7默认TLS1.1,目标服务器jdk1.8使用TLS1.2

解决方式如下:强制将请求提升至TLS1.2

/**
	 * Post(Json)
	 * @param url 
	 * @param json	请求json
	 * @param charSet	请求编码(UTF-8)
	 * @return
	 * @throws Exception
	 */
public static String sendHttpsPostjson(String url, String json,
			String charSet) throws Exception {
		// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
	    X509TrustManager trustManager = new X509TrustManager() {	 
	        @Override
	        public X509Certificate[] getAcceptedIssuers() {
	            return null;
	        }

			@Override
			public void checkClientTrusted(X509Certificate[] arg0, String arg1)
					throws CertificateException {
				
			}

			@Override
			public void checkServerTrusted(X509Certificate[] arg0, String arg1)
					throws CertificateException {
				
			}
	 
	    };
	 
	    SSLContext sc = SSLContext.getInstance("TLSv1.2");
	    sc.init(null, new TrustManager[] { trustManager }, null);	 
	    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc);

		
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
		httpPost = new HttpPost(url);
		JSONObject map = JSON.parseObject(json);
		StringEntity entity = new StringEntity(map.toString(), charSet);
		entity.setContentEncoding(charSet);
		entity.setContentType("application/json");
		httpPost.setEntity(entity);
		HttpResponse response = httpClient.execute(httpPost);
		if (response != null) {
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				result = EntityUtils.toString(resEntity, charSet);
			}
		}

		return result;
	}

/**
* Post(form表单)
* @param url
* @param params
* @return
* @throws Exception
*/
public static String sendHttpsPostForm(String url, Map<String, String> params)
throws Exception {
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

		@Override
		public void checkServerTrusted(X509Certificate[] chain,
				String authType) throws CertificateException {
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

	};

	SSLContext sc = SSLContext.getInstance("TLSv1.2");
	sc.init(null, new TrustManager[] { trustManager }, null);

	SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc);
	CloseableHttpClient httpclient = HttpClients.custom()
			.setSSLSocketFactory(sslsf).build();
	HttpPost post = new HttpPost(url);
	post.addHeader("Content-Type",
			"application/x-www-form-urlencoded; charset=UTF-8");
	post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
			"UTF-8");
	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	Set set = params.keySet();
	for (Object str : set) {
		nameValuePairs.add(new BasicNameValuePair(str.toString(), params
				.get(str)));
	}
	post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

	CloseableHttpResponse response = httpclient.execute(post);
	String returnStr = "";
	try {
		HttpEntity entity = response.getEntity();
		returnStr = EntityUtils.toString(entity);
	} finally {
		response.close();
	}
	return returnStr;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值