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) {
}
}
}
}