瀏覽代碼

修订受信HTTPS问题

Airhead 8 年之前
父節點
當前提交
2b393131ab

+ 3 - 3
hos-http/src/main/java/com/yihu/hos/http/HTTPClientDefaultImpl.java

@ -30,11 +30,11 @@ import java.util.Map;
/**
 * @created Airhead 2016/8/24.
 */
public class HTTPClientDefaultImpl implements HTTPClient {
class HTTPClientDefaultImpl implements HTTPClient {
    private static final Log log = LogFactory.getLog(HTTPClientDefaultImpl.class);
    protected CloseableHttpClient httpClient;
    protected HttpClientContext httpClientContext;
    private CloseableHttpClient httpClient;
    private HttpClientContext httpClientContext;
    HTTPClientDefaultImpl(CloseableHttpClient httpClient, CredentialsProvider credentialsProvider) {
        this.httpClient = httpClient;

+ 3 - 1
hos-http/src/main/java/com/yihu/hos/http/HTTPClientImpl.java

@ -4,9 +4,11 @@ import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.HttpClients;
/**
 * 用于HTTP和受信证书的HTTPS访问
 *
 * @created Airhead 2016/8/24.
 */
public class HTTPClientImpl extends HTTPClientDefaultImpl {
class HTTPClientImpl extends HTTPClientDefaultImpl {
    HTTPClientImpl() {
        super(HttpClients.createDefault(), null);
    }

+ 3 - 1
hos-http/src/main/java/com/yihu/hos/http/HTTPSClientImpl.java

@ -5,9 +5,11 @@ import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
/**
 * 用于非受信证书的HTTPS访问
 *
 * @created Airhead 2016/8/24.
 */
public class HTTPSClientImpl extends HTTPClientDefaultImpl {
class HTTPSClientImpl extends HTTPClientDefaultImpl {
    HTTPSClientImpl(SSLConnectionSocketFactory sslConnectionSocketFactory) {
        super(HttpClients.custom()
                .setSSLSocketFactory(sslConnectionSocketFactory)

+ 9 - 1
hos-http/src/main/java/com/yihu/hos/http/HttpClientKit.java

@ -40,6 +40,14 @@ public class HttpClientKit {
        return new HTTPClientImpl();
    }
    /**
     * 用于无认证和受信证书
     * @return HTTPClient
     */
    public static HTTPClient getHttpClient() {
        return new HTTPClientImpl();
    }
    private static HTTPClient getHttpClient(String host, Integer port) {
        String key = host + ":" + port;
        CredentialsProvider credentialsProvider = credentialsProviderMap.get(key);
@ -54,7 +62,7 @@ public class HttpClientKit {
        String key = host + ":" + port;
        SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryMap.get(key);
        if (sslConnectionSocketFactory == null) {
            throw new IllegalArgumentException("must add trust store first.");
            throw new IllegalArgumentException("Custom SSL access must add trust store first.");
        }
        CredentialsProvider credentialsProvider = credentialsProviderMap.get(key);

+ 9 - 1
hos-http/src/test/java/com/yihu/hos/http/HttpClientKitTest.java

@ -9,7 +9,15 @@ public class HttpClientKitTest {
    @Test
    public void use() throws Exception {
        HTTPResponse httpResponse = HttpClientKit.use("http://www.baidu.com").get("http://www.baidu.com");
        if (httpResponse.statusCode==200){
            System.out.println(httpResponse.body);
        }
        HTTPResponse httpResponse2 = HttpClientKit.getHttpClient().get("https://www.baidu.com");
        if (httpResponse.statusCode==200){
            System.out.println(httpResponse.body);
        }
    }
    @Test