一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
2017-12-01 15:12
447 查看
一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
import com.qunar.payment.gateway.front.channel.mpgs.po.HttpReqEntity; import org.apache.http.HttpHeaders; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.http.HttpMethod; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.X509Certificate; /** * User:xfyou Date:2017/11/23 10:50 */ public class HttpUtil { private HttpUtil() { } private static CredentialsProvider credentialsProvider; private static final SSLConnectionSocketFactory SOCKET_FACTORY = getSocketFactory(); private static final NoopHostnameVerifier NO_OP = new NoopHostnameVerifier(); public static String execute(HttpReqEntity httpReqEntity) throws IOException { CloseableHttpClient httpclient = getClosableHttpClient(httpReqEntity); CloseableHttpResponse response = null; try { response = httpclient.execute(getHttpUriRequest(httpReqEntity)); return EntityUtils.toString(response.getEntity()); } finally { try { if (null != response) response.close(); if (null != httpclient) httpclient.close(); } catch (IOException ignored) { } } } private static TrustManager getTrustManagers() { return new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } }; } private static SSLConnectionSocketFactory 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()}, new SecureRandom()); } catch (KeyManagementException e) { return null; } return new SSLConnectionSocketFactory(sslContext); } private static CloseableHttpClient getClosableHttpClient(HttpReqEntity entity) { return HttpClients.custom() .setSSLSocketFactory(SOCKET_FACTORY) .setSSLHostnameVerifier(NO_OP) .setDefaultCredentialsProvider(getCredentialsProvider(entity.getCredUserName(), entity.getCredPasswd())) .build(); } private static HttpPut getHttpPut(String requestUrl, String requestMessage, RequestConfig config) { HttpPut httpPut = new HttpPut(requestUrl); httpPut.setConfig(config); httpPut.addHeader(HttpHeaders.CONTENT_TYPE, MpgsConstant.CONTENTTYPE_JSON); httpPut.setEntity(new StringEntity(requestMessage, MpgsConstant.CHARSET_UTF8)); return httpPut; } private static HttpGet getHttpGet(String requestUrl, RequestConfig config) { HttpGet httpGet = new HttpGet(requestUrl); httpGet.setConfig(config); return httpGet; } private static HttpUriRequest getHttpUriRequest(HttpReqEntity entity) { return entity.getHttpMethod() == HttpMethod.GET ? getHttpGet(entity.getRequestUrl(), entity.getRequestConfig()) : getHttpPut(entity.getRequestUrl(), entity.getRequestMessage(), entity.getRequestConfig()); } private static CredentialsProvider getCredentialsProvider(String userName, String passwd) { if (null == credentialsProvider) { synchronized (HttpUtil.class) { if (null == credentialsProvider) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, passwd)); credentialsProvider = credsProvider; } } } return credentialsProvider; } }
相关文章推荐
- 使用Apache中的HttpClient的实例CloseableHttpClient进行http请求
- java使用httpclient封装post请求和get的请求
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式(转)
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- 一个使用HttpClient get post请求的实例
- android使用Apache HttpClient实现get/post的请求
- Springboot+Junit测试Http请求实例包括Get,Post,Put和Delete已及断言Assert的使用
- 接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
- java使用httpclient封装post请求和get的请求
- C#使用HttpClient进行GET&POST
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- C++ 使用 curl 进行 http 请求(GET、POST、Download)的封装
- 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- Android——使用HttpClient的HttpGet和Post方式进行网络通信
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- C++使用libcurl做HttpClient(业务观摩,用C++封装过程式代码,post和get的数据,最好url编码,否则+会变成空格)good
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载