创建HttpClient访问http或https,并且支持代理访问

创建HttpClient访问http或https,并且支持代理访问

创建一个HttpClient可以直接访问http请求,但如果要访问https,HttpClient创建过程要稍微复杂一点,要绕过SSL验证。能访问https的HttpClient一定可以访问http,所以这里创建的工具类既可以访问https,又可以访问http,并且支持代理访问

工具类代码
package com.day0527;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
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.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public abstract class HttpClientUtil {

    private static final Logger LOG = LoggerFactory.getLogger(HttpClientUtil.class);

    /**
     * 绕过验证
     * SSLContext
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    private static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sc = SSLContext.getInstance("SSLv3");

        // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sc.init(null, new TrustManager[] { trustManager }, null);
        return sc;
    }

    public static HttpClient getHttpClient(){
        try {
            //采用绕过验证的方式处理https请求
            SSLContext sslcontext = createIgnoreVerifySSL();

            //设置协议http和https对应的处理socket链接工厂的对象
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.INSTANCE)
                    .register("https", new SSLConnectionSocketFactory(sslcontext))
                    .build();
            PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();

            return httpClient;
        } catch (Exception e) {
            LOG.error("",e);
            return null;
        }
    }

    public static HttpClient getHttpClient(CredentialsProvider credentialsProvider){
        try {
            //采用绕过验证的方式处理https请求
            SSLContext sslcontext = createIgnoreVerifySSL();

            //设置协议http和https对应的处理socket链接工厂的对象
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.INSTANCE)
                    .register("https", new SSLConnectionSocketFactory(sslcontext))
                    .build();
            PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

            CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connManager).build();

            return httpClient;
        } catch (Exception e) {
            LOG.error("",e);
            return null;
        }
    }

    public static InputStream getStreamByUrl(HttpUriRequest request) throws IOException {
        HttpClient httpClient = getHttpClient();
        HttpResponse httpResponse = httpClient.execute(request);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream in = httpEntity.getContent();
        return in;
    }

    public static InputStream getStreamByUrl(CredentialsProvider credentialsProvider,HttpUriRequest request) throws IOException {
        HttpClient httpClient = getHttpClient(credentialsProvider);
        HttpResponse httpResponse = httpClient.execute(request);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream in = httpEntity.getContent();
        return in;
    }

}

代理服务器设置
package com.day0601;

import com.day0527.HttpClientUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.junit.Test;

import java.io.InputStream;

public class Demo3 {

    @Test
    public void test1() throws Exception{
        HttpGet httpGet = new HttpGet("http://cip.cc");
        InputStream in = HttpClientUtil.getStreamByUrl(httpGet);
        System.out.println(IOUtils.toString(in));
    }

    @Test
    public void test2() throws Exception{
        HttpHost proxy = new HttpHost("proxy host", 85,"http");
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        AuthScope authScope = new AuthScope(proxy);
        Credentials credentials = new UsernamePasswordCredentials("proxy userName", "proxy password");
        credentialsProvider.setCredentials(authScope,credentials);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                .setSocketTimeout(5000).setProxy(proxy).build();

        HttpGet httpGet = new HttpGet("http://cip.cc");
        httpGet.setConfig(requestConfig);

        InputStream in = HttpClientUtil.getStreamByUrl(credentialsProvider,httpGet);
        System.out.println(IOUtils.toString(in));
    }

}

测试



创建HttpClient访问http或https,并且支持代理访问_第1张图片
创建HttpClient访问http或https,并且支持代理访问_第2张图片
创建HttpClient访问http或https,并且支持代理访问_第3张图片
创建HttpClient访问http或https,并且支持代理访问_第4张图片

你可能感兴趣的:(java,https,http)