import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
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.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpsUtil {
private static final Logger LOG = LoggerFactory.getLogger(HttpsUtil.class);
public static final String CHARACTER_ENCODING = "UTF-8";
public static final String CONTENT_TYPE = "Content-Type";
public static final int CONNECTION_TIMEOUT = 10000;
public static final int SO_TIMEOUT = 30000;
public HttpsUtil() {
}
public static HttpsUtil.HttpResponseWrapper requestGetMethod(String url, Map<String, String> requestParas) {
CloseableHttpClient httpClient = getCloseableHttpClient(url);
try {
StringBuffer e = new StringBuffer(url);
if(MapUtils.isNotEmpty(requestParas)) {
StringBuffer httpGet = new StringBuffer();
Iterator response = requestParas.keySet().iterator();
while(response.hasNext()) {
String key = (String)response.next();
httpGet.append(key);
httpGet.append("=");
httpGet.append((String)requestParas.get(key));
httpGet.append("&");
}
httpGet.setLength(httpGet.length() - 1);
if(url.contains("?")) {
e.append("&");
} else {
e.append("?");
}
e.append(httpGet.toString());
}
HttpGet httpGet1 = new HttpGet(e.toString());
CloseableHttpResponse response1 = httpClient.execute(httpGet1);
return new HttpsUtil.HttpResponseWrapper(httpClient, response1);
} catch (IOException var7) {
var7.printStackTrace();
return null;
}
}
private static CloseableHttpClient getCloseableHttpClient(String url) {
CloseableHttpClient httpClient = null;
if(url.startsWith("https://")) {
httpClient = createHttpsClient();
} else {
httpClient = createHttpClient();
}
return httpClient;
}
public static HttpsUtil.HttpResponseWrapper requestPostMethod(String url, Map<String, String> requestParas) {
return requestPostMethod(url, (Map)null, requestParas, "UTF-8", (String)null);
}
public static HttpsUtil.HttpResponseWrapper requestPostMethod(String url, Map<String, String> requestHeaders, Map<String, String> requestParas, String requestCharacter, String xForWardedFor) {
CloseableHttpClient httpClient = getCloseableHttpClient(url);
HttpPost httpPost = new HttpPost(url);
try {
if(StringUtils.isNotEmpty(xForWardedFor)) {
httpPost.addHeader("X-Forwarded-For", xForWardedFor);
}
initHeader(httpPost, requestHeaders);
if(MapUtils.isNotEmpty(requestParas)) {
List e = initNameValuePair(requestParas);
httpPost.setEntity(new UrlEncodedFormEntity(e, requestCharacter));
}
CloseableHttpResponse e1 = httpClient.execute(httpPost);
return new HttpsUtil.HttpResponseWrapper(httpClient, e1);
} catch (IOException var8) {
var8.printStackTrace();
return null;
}
}
private static void initHeader(HttpMessage httpPost, Map<String, String> requestHeaders) {
if(MapUtils.isNotEmpty(requestHeaders)) {
Iterator var2 = requestHeaders.keySet().iterator();
while(var2.hasNext()) {
String key = (String)var2.next();
httpPost.addHeader(key, (String)requestHeaders.get(key));
}
}
}
public static HttpsUtil.HttpResponseWrapper requestPostMethod(String url, Map<String, String> requestParas, String requestCharacter, String xForWardedFor) {
return requestPostMethod(url, (Map)null, requestParas, requestCharacter, (String)null);
}
public static HttpsUtil.HttpResponseWrapper requestPutMethod(String url, Map<String, String> requestParas, String requestCharacter) {
CloseableHttpClient httpClient = getCloseableHttpClient(url);
HttpPut httpPut = new HttpPut(url);
try {
if(MapUtils.isNotEmpty(requestParas)) {
List e = initNameValuePair(requestParas);
httpPut.setEntity(new UrlEncodedFormEntity(e, requestCharacter));
}
CloseableHttpResponse e1 = httpClient.execute(httpPut);
return new HttpsUtil.HttpResponseWrapper(httpClient, e1);
} catch (IOException var6) {
var6.printStackTrace();
return null;
}
}
public static HttpsUtil.HttpResponseWrapper requestDeleteMethod(String url) {
CloseableHttpClient httpClient = getCloseableHttpClient(url);
HttpDelete httpDelete = new HttpDelete(url);
try {
CloseableHttpResponse ex = httpClient.execute(httpDelete);
return new HttpsUtil.HttpResponseWrapper(httpClient, ex);
} catch (IOException var4) {
var4.printStackTrace();
return null;
}
}
public static List<NameValuePair> initNameValuePair(Map<String, String> params) {
ArrayList formParams = new ArrayList();
if(params != null && params.size() > 0) {
ArrayList keys = new ArrayList(params.keySet());
Collections.sort(keys);
Iterator var3 = keys.iterator();
while(var3.hasNext()) {
String key = (String)var3.next();
formParams.add(new BasicNameValuePair(key, (String)params.get(key)));
}
}
return formParams;
}
private void close(CloseableHttpClient httpClient) {
if(httpClient != null) {
try {
httpClient.close();
} catch (IOException var3) {
var3.printStackTrace();
}
}
}
public static CloseableHttpClient createHttpsClient() {
return createHttpsClient(10000, 30000);
}
public static CloseableHttpClient createHttpsClient(int connectionTimeout, int soTimeout) {
SSLContext sslcontext = SSLContexts.createDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, (String[])null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout).build();
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfig).build();
return httpclient;
}
public static CloseableHttpClient createHttpClient() {
return createHttpsClient(10000, 30000);
}
public static CloseableHttpClient createHttpClient(int connectionTimeout, int soTimeout) {
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout).build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
return httpclient;
}
public static void main(String[] args) throws IOException {
String url = "https://www.baidu.com/";
HashMap maps = new HashMap();
maps.put("name", "11111");
HttpsUtil.HttpResponseWrapper wrapper = requestGetMethod(url, (Map)null);
System.out.println(wrapper.getResponseString());
wrapper.close();
}
public static class HttpResponseWrapper implements Closeable {
private HttpResponse httpResponse;
private HttpClient httpClient;
public HttpResponseWrapper(HttpClient httpClient, HttpResponse httpResponse) {
this.httpClient = httpClient;
this.httpResponse = httpResponse;
}
public HttpResponseWrapper(HttpClient httpClient) {
this.httpClient = httpClient;
}
public HttpResponse getHttpResponse() {
return this.httpResponse;
}
public void setHttpResponse(HttpResponse httpResponse) {
this.httpResponse = httpResponse;
}
public InputStream getResponseStream() throws IllegalStateException, IOException {
return this.httpResponse.getEntity().getContent();
}
public String getResponseString(String responseCharacter) throws ParseException, IOException {
HttpEntity entity = this.getEntity();
String responseStr = EntityUtils.toString(entity, responseCharacter);
if(entity.getContentType() == null) {
responseStr = new String(responseStr.getBytes("iso-8859-1"), responseCharacter);
}
EntityUtils.consume(entity);
return responseStr;
}
public String getResponseString() throws ParseException, IOException {
return this.getResponseString("UTF-8");
}
public int getStatusCode() {
return this.httpResponse.getStatusLine().getStatusCode();
}
public int getStatusCodeAndClose() {
this.close();
return this.getStatusCode();
}
public HttpEntity getEntity() {
return this.httpResponse.getEntity();
}
public void close() {
if(this.httpClient instanceof CloseableHttpClient) {
try {
((CloseableHttpClient)this.httpClient).close();
} catch (IOException var2) {
var2.printStackTrace();
}
}
}
}
}
httpUtil
最新推荐文章于 2021-10-13 22:39:05 发布