HTTP-GET带header请求
// authorization为授权验证,若无授权验证可删除
private static JSONObject sendGet(String url, String authorization) throws IOException {
CloseableHttpClient cHttpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
get.addHeader("Content-Type", "application/json");
get.addHeader("Authorization", authorization);
CloseableHttpResponse response = cHttpClient.execute(get);
logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
logger.info("URL=" + url + ",response=" + responseContent);
response.close();
cHttpClient.close();
return JSONObject.parseObject(responseContent);
}
HTTP-POST带header请求
private static JSONObject sendPost(String businessUrl, JSONObject sendMsgBody, String accessToken) throws IOException {
CloseableHttpClient cHttpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(businessUrl);
post.addHeader("Content-Type", "application/json");
post.addHeader("Authorization", "Bearer " + accessToken);
post.setEntity(new StringEntity(sendMsgBody.toString(), "UTF-8"));
CloseableHttpResponse response = cHttpClient.execute(post);
logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
logger.info("URL=" + businessUrl + ",response=" + responseContent);
response.close();
cHttpClient.close();
return JSONObject.parseObject(responseContent);
}
HTTPS-POST带header请求
//设置链接超时和请求超时等参数,否则会长期停止或者崩溃
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
public static String sendHttpsPost(String url, JSONObject params) {
String responseContent = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {
HttpPost httpPost = new HttpPost(url);
// header
httpPost.addHeader("AppKey", SystemConstants.APP_KEY);
httpPost.addHeader("Secret", SystemConstants.SECRET);
// body
httpPost.setEntity(new StringEntity(params.toString(), "UTF-8"));
httpClient = HttpClients.custom().setSSLSocketFactory(createSslConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* 创建SSL安全连接
* @return SSLConnectionSocketFactory
*/
private static SSLConnectionSocketFactory createSslConnSocketFactory() {
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
@Override
public void verify(String host, SSLSocket ssl) {
}
@Override
public void verify(String host, X509Certificate cert) {
}
@Override
public void verify(String host, String[] cns, String[] subjectAlts) {
}
});
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return sslsf;
}