JDK1.7 解决访问Https域名报shakehand_failure的问题

public static String sendHttpsPost(String url, Map<String, String> headerMap, List<BasicNameValuePair> nvps,
			String contentType, String charSet,String a) 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();
		System.out.println(url+"-----------------------------------------");

		HttpPost post = new HttpPost(url);
		if (null != headerMap && !headerMap.isEmpty()) {
			Set<String> keySet = headerMap.keySet();
			for (String key : keySet) {
				String v = headerMap.get(key);
				if (StringUtils.isNotEmpty(v)) {
					post.addHeader(key, v);
				}
			}
		}
		post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charSet);
		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(nvps,"UTF-8"));

/*		post.setEntity(new StringEntity(params.toString(), "UTF-8"));
*/		
		CloseableHttpResponse response = httpclient.execute(post);
		
		String result="";
		if (response != null) {
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				try {
					result = EntityUtils.toString(resEntity, charSet);
					System.out.println(result);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return result;
	}
 	List<BasicNameValuePair> nvps = new ArrayList<>();
    			nvps.add(new BasicNameValuePair("client_id",PropertiesUtil.getLCDJByKey("client_ID")));
    			nvps.add(new BasicNameValuePair("client_secret",PropertiesUtil.getLCDJByKey("client_SERCRET")));
    			nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
    			nvps.add(new BasicNameValuePair("code", code));
    			nvps.add(new BasicNameValuePair("state", state));
    			nvps.add(new BasicNameValuePair("redirect_uri", PropertiesUtil.getLCDJByKey("redirect_URI")));


	            System.out.println("======="+nvps.toString());
                    Response post = post18(PropertiesUtil.getLCDJByKey("accessTokenURL"), nvps, false);
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值