日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

發(fā)布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一個封裝的使用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;} }

?

總結(jié)

以上是生活随笔為你收集整理的一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。