static int timeout=10000;
public static String doGetWithAuthorization(String url,String authorization){
String temp=url.substring(0,7).replace(" ","").toLowerCase();
if(temp.indexOf("https")>-1){
return doHttpsGetWithAuthorization(url,authorization);
}else {
return doHttpGetWithAuthorization(url,authorization);
}
}
public static String doPostWithContentType(String url,String paramBody,String contentType){
String temp=url.substring(0,7).replace(" ","").toLowerCase();
if(temp.indexOf("https")>-1){
return doHttpsPostWithContentType(url,paramBody,contentType);
}else {
return doHttpPostWithContentType(url,paramBody,contentType);
}
}
public static String doHttpsPostWithContentType(String url,String paramBody,String contentType){
logger.info("Httpspost发送url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType);
CloseableHttpClient httpClient = createSSLClientDefault();
String returnValue=null;
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try{
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpPost.setConfig(requestConfig);
StringEntity requestEntity = new StringEntity(paramBody,"utf-8");
requestEntity.setContentEncoding("UTF-8");
httpPost.setHeader("Content-type", contentType);
httpPost.setEntity(requestEntity);
returnValue = httpClient.execute(httpPost,responseHandler);
logger.info("Httpspost接收url:"+url+",请求参数:"+paramBody+",返回值:"+returnValue);
} catch(Exception e){
e.printStackTrace();
logger.info("Httpspost异常,url:"+url+",请求参数:"+paramBody+",异常信息:"+e.toString());
} finally {
close(httpClient);
}
return returnValue;
}
public static String doHttpPostWithContentType(String url,String paramBody,String contentType){
logger.info("Httppost发送url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType);
CloseableHttpClient httpClient=null;
String returnValue=null;
try{
httpClient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpPost.setConfig(requestConfig);
StringEntity requestEntity = new StringEntity(paramBody,"utf-8");
requestEntity.setContentEncoding("UTF-8");
httpPost.setHeader("Content-type", contentType);
httpPost.setEntity(requestEntity);
returnValue = httpClient.execute(httpPost,responseHandler);
logger.info("Httppost接收url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType+",返回值:"+returnValue);
}catch (Exception e){
e.printStackTrace();
logger.info("Httppost异常,url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType+",异常信息:"+e.toString());
}finally {
close(httpClient);
}
return returnValue;
}
public static String doHttpsGetWithAuthorization(String url,String authorization){
logger.info("HttpsGet发送url:"+url);
CloseableHttpClient httpClient = createSSLClientDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String returnValue=null;
try {
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpGet.setConfig(requestConfig);
httpGet.setHeader("Authorization",authorization);
returnValue = httpClient.execute(httpGet,responseHandler);
logger.info("HttpsGet接收url:"+url+",返回值:"+returnValue);
} catch (Exception e) {
e.printStackTrace();
logger.info("HttpsGet异常,url:"+url+",异常信息:"+e.toString());
} finally {
close(httpClient);
}
return returnValue;
}
public static String doHttpGetWithAuthorization(String url,String authorization){
logger.info("HttpGet发送url:"+url);
CloseableHttpClient httpClient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String returnValue=null;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpGet.setConfig(requestConfig);
httpGet.setHeader("Authorization",authorization);
returnValue = httpClient.execute(httpGet,responseHandler);
logger.info("HttpGet接收url:"+url+",返回值:"+returnValue);
} catch (Exception e) {
e.printStackTrace();
logger.info("HttpGet异常,url:"+url+",返回值:"+returnValue+",异常信息:"+e.toString());
} finally {
close(httpClient);
}
return returnValue;
}
public static CloseableHttpClient createSSLClientDefault() {
CloseableHttpClient closeableHttpClient;
try {
SSLContext sslContext = new org.apache.http.ssl.SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
closeableHttpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
logger.info("SSLHttpClient创建成功");
return closeableHttpClient;
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return null;
}
public static void close(CloseableHttpClient httpClient){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("http关闭异常");
}
}