public static String sendHttpsPost(String url, Map<String, String> headerMap, List<BasicNameValuePair> nvps,
String contentType, String charSet,String a) throws Exception {
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>();
post.setEntity(new UrlEncodedFormEntity(nvps,"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) {
e.printStackTrace();
} catch (IOException e) {
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);