HttpClient 使用代理完全版本

import com.unionpay.spiderframework.spiderframework.common.util.Config;

import org.apache.commons.io.IOUtils;

import org.apache.http.*;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.GzipDecompressingEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

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.LayeredConnectionSocketFactory;

import org.apache.http.conn.socket.PlainConnectionSocketFactory;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.impl.client.*;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.apache.http.message.BasicNameValuePair;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

public class IHttpClient {

private final static Logger LOGGER = LoggerFactory.getLogger(IHttpClient.class);
private static PoolingHttpClientConnectionManager cm = null;
// 代理服务器
private static final String ProxyHost = Config.get("proxy.host");
private static final Integer ProxyPort = Config.getInt("proxy.port");
// 代理隧道验证信息
private static final String ProxyUser = Config.get("proxy.user");
private static final String ProxyPass = Config.get("proxy.passord");
private static CredentialsProvider credsProvider = null;
private static RequestConfig requestConfig = null;

static {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", plainsf)
            .register("https", sslsf)
            .build();
    cm = new PoolingHttpClientConnectionManager(registry);
    cm.setMaxTotal(Config.getInt("httpclient.pool.max.connection"));
    cm.setDefaultMaxPerRoute(Config.getInt("httpclient.pool.max.route"));

    credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(ProxyHost, ProxyPort),
            new UsernamePasswordCredentials(ProxyUser, ProxyPass));

    requestConfig = RequestConfig.custom()
            .setConnectTimeout(100000)
            .setConnectionRequestTimeout(Config.getInt("httpclient.request.timeout"))
            .setSocketTimeout(Config.getInt("httpclient.socket.timeout"))
            .setProxy(new HttpHost("120.52.73.98", 8082))
            .build();

}

/**
 * 处理response
 *
 * [@param](https://my.oschina.net/u/2303379) httpResponse
 * [@return](https://my.oschina.net/u/556800)
 */
private static CloseableHttpResponse handleResponse(CloseableHttpResponse httpResponse) {

    Header ceheader = httpResponse.getEntity().getContentEncoding();
    if (ceheader != null) {
        HeaderElement[] codecs = null;
        codecs = ceheader.getElements();
        for (int i = 0; i < codecs.length; i++) {
            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                httpResponse.setEntity(new GzipDecompressingEntity(httpResponse.getEntity()));
            }
        }
    }
    return httpResponse;
}

public static void main(String[] args) throws IOException {
    HttpGet httpGet = new HttpGet("http://1212.ip138.com/ic.asp");
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("method", "next"));
    nvps.add(new BasicNameValuePair("params", "{\"topic_id\":1740,\"offset\":0,\"hash_id\":\"1\"}"));
    boolean flag = true;
    CloseableHttpClient httpClient = null;
    httpGet.setConfig(requestConfig);
    if (!flag) {
        httpClient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
                .setDefaultCredentialsProvider(credsProvider)
                .setConnectionManager(cm).build();
    } else {
        httpClient = HttpClientBuilder.create().setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
                .setConnectionManager(cm)
                .build();
    }
    CloseableHttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            System.out.println("第{}次请求{}返回{}");
        } else {
            handleResponse(httpResponse);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream in = httpEntity.getContent();
            String response = IOUtils.toString(in, "gb2312");
            in.close();
            System.out.println("aaaaaaaaaa" + response);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (httpResponse != null)
                httpResponse.close();
        } catch (IOException e) {
        }

    }
}

}

转载于:https://my.oschina.net/u/2523402/blog/823197

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值