Java代码
package util;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.*;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
/**
* @author zoult on 2018/6/7
*/
public class HttpClientUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class);
private static CloseableHttpClient httpClient = null;
public HttpClientUtil() {
}
public static String doGet(String url) throws HttpException {
return doGetForString(url, new HashMap());
}
public static InputStream doGetForStream(String url) throws HttpException {
return doGetForStream(url, new HashMap());
}
public static String doGetForString(String url, Map<String, String> params) throws HttpException {
HttpResponse response = doGet(url, paramsConverter(params), (List) null);
if (response.getStatusLine().getStatusCode() != 200) {
throw new HttpException(
"HttpClientUtil#doGetForString - 请求返回状态码异常 : " + response.getStatusLine().getStatusCode());
} else {
return readResponseContent(response);
}
}
public static InputStream doGetForStream(String url, Map<String, String> queryParams) throws HttpException {
HttpResponse response = doGet(url, queryParams);
if (response.getStatusLine().getStatusCode() != 200) {
throw new HttpException(
"HttpClientUtil#doGetForStream - 请求返回状态码异常 : " + response.getStatusLine().getStatusCode());
} else {
return readInputStream(response);
}
}
public static HttpResponse doGet(String url, Map<String, String> queryParams) throws HttpException {
return doGet(url, paramsConverter(queryParams), (List) null);
}
public static HttpResponse doGet(String url, List<NameValuePair> queryParams, List<Header> headers)
throws HttpException {
HttpGet method = new HttpGet();
builderRequestMethodURI(method, url,