java网络编程____httpclient多线程请求和线程池
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class ThreadGroupContext extends Thread {
private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
public ThreadGroupContext(CloseableHttpClient httpClient, HttpGet httpget) {
this.httpClient = httpClient;
this.context = HttpClientContext.create();
this.httpget = httpget;
}
@Override
public void run() {
try {
CloseableHttpResponse response = httpClient.execute(httpget,context);
try {
HttpEntity entity = response.getEntity();
System.out.println("结果集转string串____:"+EntityUtils.toString(entity));
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
@SuppressWarnings("unused")
public class HttpClientManyThread {
private static void manyThreadHttpRequest() throws Exception {
//httpclient线程池管理器
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
//最大练级数
connManager.setMaxTotal(200);
//设置每个路由连接数
connManager.setDefaultMaxPerRoute(3);
//针对的主机
HttpHost host = new HttpHost("http://blog.csdn.net/qq827245563");
//每个路由映射的最大的连接数
connManager.setMaxPerRoute(new HttpRoute(host), 5);
//创建httpclisent连接
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
String[] urls = { "http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563",
"http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563",
"http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563", "http://blog.csdn.net/qq827245563",
"http://blog.csdn.net/qq827245563" };
ThreadGroupContext[] threadGroupContexts = new ThreadGroupContext[10];
for (int i = 0; i < threadGroupContexts.length; i++) {
HttpGet httpget = new HttpGet(urls[i]);
threadGroupContexts[i] = new ThreadGroupContext(httpClient, httpget);
}
for (int j = 0; j < threadGroupContexts.length; j++) {
threadGroupContexts[j].start();
}
for (int j = 0; j < threadGroupContexts.length; j++) {
threadGroupContexts[j].join();
}
}
public static void main(String[] args) throws Exception {
manyThreadHttpRequest();
}
}
//
运行结果