java httpclient put_一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。详解...

importcom.qunar.payment.gateway.front.channel.mpgs.po.HttpReqEntity;importorg.apache.http.HttpHeaders;importorg.apache.http.auth.AuthScope;importorg.apache.http.auth.UsernamePasswordCredentials;importorg.apache.http.client.CredentialsProvider;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPut;importorg.apache.http.client.methods.HttpUriRequest;importorg.apache.http.conn.ssl.NoopHostnameVerifier;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.BasicCredentialsProvider;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importorg.springframework.http.HttpMethod;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importjava.io.IOException;importjava.security.KeyManagementException;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjava.security.cert.X509Certificate;/*** User:xfyou Date:2017/11/23 10:50*/

public classHttpUtil {privateHttpUtil() {

}private staticCredentialsProvider credentialsProvider;private static final SSLConnectionSocketFactory SOCKET_FACTORY =getSocketFactory();private static final NoopHostnameVerifier NO_OP = newNoopHostnameVerifier();public static String execute(HttpReqEntity httpReqEntity) throwsIOException {

CloseableHttpClient httpclient=getClosableHttpClient(httpReqEntity);

CloseableHttpResponse response= null;try{

response=httpclient.execute(getHttpUriRequest(httpReqEntity));returnEntityUtils.toString(response.getEntity());

}finally{try{if (null !=response) response.close();if (null !=httpclient) httpclient.close();

}catch(IOException ignored) {

}

}

}private staticTrustManager getTrustManagers() {return newX509TrustManager() {publicX509Certificate[] getAcceptedIssuers() {return null;

}public voidcheckClientTrusted(X509Certificate[] certs, String authType) {

}public voidcheckServerTrusted(X509Certificate[] certs, String authType) {

}

};

}private staticSSLConnectionSocketFactory getSocketFactory() {

SSLContext sslContext;try{

sslContext=SSLContext.getInstance(MpgsConstant.SECURITYPROTOCOL_VERSION_TLS_1_2);

}catch(NoSuchAlgorithmException e) {return null;

}try{

sslContext.init(null, new TrustManager[]{getTrustManagers()}, newSecureRandom());

}catch(KeyManagementException e) {return null;

}return newSSLConnectionSocketFactory(sslContext);

}private staticCloseableHttpClient getClosableHttpClient(HttpReqEntity entity) {returnHttpClients.custom()

.setSSLSocketFactory(SOCKET_FACTORY)

.setSSLHostnameVerifier(NO_OP)

.setDefaultCredentialsProvider(getCredentialsProvider(entity.getCredUserName(), entity.getCredPasswd()))

.build();

}private staticHttpPut getHttpPut(String requestUrl, String requestMessage, RequestConfig config) {

HttpPut httpPut= newHttpPut(requestUrl);

httpPut.setConfig(config);

httpPut.addHeader(HttpHeaders.CONTENT_TYPE, MpgsConstant.CONTENTTYPE_JSON);

httpPut.setEntity(newStringEntity(requestMessage, MpgsConstant.CHARSET_UTF8));returnhttpPut;

}private staticHttpGet getHttpGet(String requestUrl, RequestConfig config) {

HttpGet httpGet= newHttpGet(requestUrl);

httpGet.setConfig(config);returnhttpGet;

}private staticHttpUriRequest getHttpUriRequest(HttpReqEntity entity) {return entity.getHttpMethod() == HttpMethod.GET ?getHttpGet(entity.getRequestUrl(), entity.getRequestConfig())

: getHttpPut(entity.getRequestUrl(), entity.getRequestMessage(), entity.getRequestConfig());

}private staticCredentialsProvider getCredentialsProvider(String userName, String passwd) {if (null ==credentialsProvider) {synchronized (HttpUtil.class) {if (null ==credentialsProvider) {

CredentialsProvider credsProvider= newBasicCredentialsProvider();

credsProvider.setCredentials(AuthScope.ANY,newUsernamePasswordCredentials(userName, passwd));

credentialsProvider=credsProvider;

}

}

}returncredentialsProvider;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值