赞
踩
这里写了一个工具类 我 没有用证书 而是用忽略证书得方法。 里面包含get 与post请求
package com.hgzh.ulanqab.visualization.utils; import org.apache.commons.codec.CharEncoding; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; 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.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.TrustStrategy; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import javax.net.ssl.SSLContext; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * HttpClientUtil * 增加方法post 文本 */ public class HttpClientUtil { public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8); /** * 连接超时时间 * 20s */ private static final int CONNECT_TIMEOUT = 20000; /** * 请求超时时间 * <p> * <p> * 20s */ private static final int CONNECT_REQUEST_TIMEOUT = 20000; /** * socket超时时间 * 20s */ private static final int SOCKET_TIMEOUT = 20000; /** * 连接超时时间 * 20s */ private int connectTimeout = CONNECT_TIMEOUT; /** * 请求超时时间 * 20s */ private int connectionRequestTimeout = CONNECT_REQUEST_TIMEOUT; /** * socket超时时间 * 20s */ private int socketTimeout = SOCKET_TIMEOUT; private List<Header> headers; private Integer responseCode = -1; private static volatile HttpClientUtil instance = null; protected static Random random = new Random(); public static HttpClientUtil getInstance() { if (instance == null) { synchronized (HttpClientUtil.class) { if (instance == null) { instance = new HttpClientUtil(); } } } return instance; } /** * 关闭ssl验证 * @return */ private CloseableHttpClient getHttpClientNoSsl() { try { HttpClientBuilder b = HttpClientBuilder.create(); SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnect
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。