/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/ package org.apache.http.examples.client; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils; /**
* An example that performs GETs from multiple threads.
*
*/
public class ClientMultiThreadedExecution { public static void main(String[] args) throws Exception {
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm.setMaxTotal(100); HttpClient httpclient = new DefaultHttpClient(cm);
try {
// create an array of URIs to perform GETs on
String[] urisToGet = {
"http://hc.apache.org/",
"http://hc.apache.org/httpcomponents-core-ga/",
"http://hc.apache.org/httpcomponents-client-ga/",
"http://svn.apache.org/viewvc/httpcomponents/"
}; // create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpclient, httpget, i + 1);
} // start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
} // join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
} } finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
} /**
* A thread that performs a GET.
*/
static class GetThread extends Thread { private final HttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
private final int id; public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
this.id = id;
} /**
* Executes the GetMethod and prints some status information.
*/
@Override
public void run() { System.out.println(id + " - about to get something from " + httpget.getURI()); try { // execute the method
HttpResponse response = httpClient.execute(httpget, context); System.out.println(id + " - get executed");
// get the response body as an array of bytes
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(id + " - " + bytes.length + " bytes read");
} } catch (Exception e) {
httpget.abort();
System.out.println(id + " - error: " + e);
}
} } }

Apache HttpComponents 多线程处理HTTP请求的更多相关文章

  1. org.apache.httpcomponents httpclient 发起HTTP JSON请求

    1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...

  2. JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例

    一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ...

  3. Apache HttpComponents中的cookie匹配策略

    Apache HttpComponents中的cookie匹配策略 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre. ...

  4. httpclient工具使用(org.apache.httpcomponents.httpclient)

    httpclient工具使用(org.apache.httpcomponents.httpclient) 引入依赖 <dependency> <groupId>org.apac ...

  5. Apache HttpComponents 工具类 [ HttpUtil ]

    pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  6. Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页

    Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...

  7. 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

    一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...

  8. windows上apache是线程处理请求,linux上apache是进程处理请求

    windows上apache是线程处理请求,linux上apache是进程处理请求

  9. 彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的

    彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的 1.线程池,thread = threadPool.getThread(),thread.executeHtt ...

随机推荐

  1. 获取网站资源 getResourceAsStream

    获取网站资源(重点) public void doGet(HttpServletRequest request, HttpServletResponse response)throws Servlet ...

  2. 简单易懂的Ubuntu生存战略:开启无线网卡的wifi热点,ap模式

    在ubuntu下如何把无线网卡设置成可供wifi设备上网的接入点 因为某些移动设备对wifi热点的要求有些苛刻,p2p模式的无线连接方式虽然易设置,但是实用性不高. 相对而言,创建一个虚拟无线路由器成 ...

  3. oracle中number对应java数据类型

    本文转自:http://blog.csdn.net/ludongshun2016/article/details/71453125 数据库中为number类型的字段,在Java类型中对应的有Integ ...

  4. Python 随机数 random

    1. Python seed() 函数     seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数. seed( )是不能直接访问的,需要导入 random 模块,然后 ...

  5. Atom 检测php错误扩展linter-php

  6. Android oncreate onupgrade什么时候被调用

    在学习Android数据库SQLite之前,必须意识到这一点,目前在Android系统中集成的是SQLite3 版本,SQLite是一个开源的嵌入式数据库,他支持NULL.INTEGER.REAL.T ...

  7. linux下判断文件和目录是否存在

    1.前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在.我结合APUE第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询. 2.stat系列函数 stat函数用来返回与文 ...

  8. C语言sprintf与sscanf函数

    1.前言 我们经常涉及到数字与字符串之间的转换,例如将32位无符号整数的ip地址转换为点分十进制的ip地址字符串,或者反过来.从给定的字符串中提取相关内容,例如给定一个地址:http://www.bo ...

  9. stm32 外扩SRAM使用问题

    当把外扩SRAM内存拷贝到片上SRAM内存时使用内存拷贝函数memset()或者原子定义的mymemset()函数,编译器会提示空间不足. 原因是这两个函数一个是只能对片上SRAM操作,一个是只能对外 ...

  10. unity 查看prefab层次

    点那个箭头,可以展开: