com.zoomlion.cloud.common.util.HttpClientFactory Maven / Gradle / Ivy
The newest version!
package com.zoomlion.cloud.common.util;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class HttpClientFactory {
protected static Log log = LogFactory.getLog(HttpClientFactory.class);
private HttpClientFactory() {
}
public static CloseableHttpClient getHttpsInstance() {
return newHttpsInstance();
}
public static CloseableHttpClient getHttpInstance() {
return newHttpInstance();
}
public static CloseableHttpClient newHttpsInstance() {
CloseableHttpClient httpClient = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
httpClient = HttpClientBuilder.create().setSSLContext(sslContext).setUserAgent("Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803").build();
} catch (Exception e) {
log.error("创建https请求异常:{}", e);
}
return httpClient;
}
public static CloseableHttpClient newHttpInstance() {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClientBuilder.create().setUserAgent("Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803").build();
} catch (Exception e) {
log.error("创建http请求异常:{}", e);
}
return httpClient;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy