| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package com.pj.api.pushfee.tools;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.config.AuthSchemes;
- import org.apache.http.client.config.CookieSpecs;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.config.Registry;
- import org.apache.http.config.RegistryBuilder;
- import org.apache.http.conn.socket.ConnectionSocketFactory;
- import org.apache.http.conn.socket.PlainConnectionSocketFactory;
- import org.apache.http.conn.ssl.NoopHostnameVerifier;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.entity.BufferedHttpEntity;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.Arrays;
- public class HttpUtils {
- private static String DEFAULT_ENCODING = "UTF-8";
- private static HttpClient getClientNoSSL() {
- try {
- SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
- ctx.init(null, new TrustManager[] { new MyTrustManager() }, null);
- SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
- // 创建Registry
- RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
- .setExpectContinueEnabled(Boolean.TRUE).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM,AuthSchemes.DIGEST))
- .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
- Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.INSTANCE)
- .register("https",socketFactory).build();
- // 创建ConnectionManager,添加Connection配置信息
- PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
- HttpClient closeableHttpClient = HttpClients.custom().setConnectionManager(connectionManager)
- .setDefaultRequestConfig(requestConfig).build();
- return closeableHttpClient;
- } catch (KeyManagementException e) {
- throw new RuntimeException(e);
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- }
- /***
- * 提交具有加密的https或http Post请求
- * @param url 请求url
- * @param jsonEntity 请求体
- * @throws IOException
- */
- public static String doPostWithOpenApi(String url, String jsonEntity) throws IOException {
- HttpClient httpClient = getClientNoSSL();
- HttpResponse response = null;
- try {
- HttpPost post = new HttpPost(url);
- post.setHeader("contentType", "application/json;charset=UTF-8");
- post.setHeader("Authorization", "openApi");
- StringEntity entity = new StringEntity(jsonEntity);
- entity.setContentEncoding(DEFAULT_ENCODING);
- entity.setContentType("application/json");
- //提交json串
- BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
- post.setEntity(bufferedHttpEntity);
- response = httpClient.execute(post);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (response.getStatusLine().getStatusCode() == 200) {
- return getResponseAsString(response);
- }else {
- return getResponseAsString(response);
- }
- }
- public static String doGet(String url) throws IOException {
- HttpClient httpClient = getClientNoSSL();
- HttpResponse response = null;
- try {
- HttpGet get = new HttpGet(url);
- get.setHeader("contentType", "application/json;charset=UTF-8");
- response = httpClient.execute(get);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (response.getStatusLine().getStatusCode() == 200) {
- return getResponseAsString(response);
- }else {
- return getResponseAsString(response);
- }
- }
- /**
- * 将response响应消息转换为字符串
- * @param response
- * @return
- * @throws IOException
- */
- public static String getResponseAsString(HttpResponse response) throws IOException {
- String result = "";
- HttpEntity resEntity = response.getEntity();
- InputStreamReader reader = new InputStreamReader(resEntity.getContent());
- char[] buff = new char[1024];
- int length = 0;
- while ((length = reader.read(buff)) != -1) {
- result += new String(buff, 0, length);
- }
- return result;
- }
- }
- /**
- * 自定义重写自动接收所有证书
- */
- class MyTrustManager implements X509TrustManager {
- public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
- }
- public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
- }
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
- }
|