论坛首页 Java企业应用论坛

HttpClient Examples:Threaded request execution

浏览 6143 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (5)
作者 正文
   发表时间:2009-01-15  

官方主頁:http://hc.apache.org/

 

Components

Threaded request execution

An example that executes HTTP requests from multiple worker threads.

 

线程请求执行

 

一个多线程程执行HTTP请求的例子。

 

package cn.lake.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

/**
 * An example that performs GETs from multiple threads.
 * 
 * @author Michael Becke
 */
public class ClientMultiThreadedExecution {

	public static void main(String[] args) throws Exception {
		// Create and initialize HTTP parameters
		HttpParams params = new BasicHttpParams();
		ConnManagerParams.setMaxTotalConnections(params, 100);
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

		// Create and initialize scheme registry 
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

		// Create an HttpClient with the ThreadSafeClientConnManager.
		// This connection manager must be used if more than one thread will
		// be using the HttpClient.
		ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
		HttpClient httpClient = new DefaultHttpClient(cm, params);

		// create an array of URIs to perform GETs on
		String[] urisToGet = { "http://jakarta.apache.org/", "http://jakarta.apache.org/commons/",
				"http://jakarta.apache.org/commons/httpclient/", "http://svn.apache.org/viewvc/jakarta/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();
		}

	}

	/**
	 * 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);
			}
		}

	}

}

 

翻譯的不好,請見諒!

   发表时间:2009-01-15   最后修改:2009-01-15
建议楼主发的"HttpClient Examples:..."系列放到“入门讨论区”去。

原因如下:
1) 开贴多,每贴有效内容少
2) 没什么原创,基本上是拷贝Apache HttpClient的原文和例子,每贴翻译一句话。相信真需要使用HttpClient的人,会去官方网站看文档,而不是来这里。

这样的帖子太多会造成Java版面质量下降,首页几乎全是这系列帖子了,原创都给挤没了。

感觉楼主在疯狂刷分,呵呵。
0 请登录后投票
   发表时间:2009-01-15  
dhxlsfn 写道

建议楼主发的"HttpClient Examples:..."系列放到“入门讨论区”去。 原因如下: 1) 开贴多,每贴有效内容少 2) 没什么原创,基本上是拷贝Apache HttpClient的原文和例子,每贴翻译一句话。相信真需要使用HttpClient的人,会去官方网站看文档,而不是来这里。 这样的帖子太多会造成Java版面质量下降,首页几乎全是这系列帖子了,原创都给挤没了。 感觉楼主在疯狂刷分,呵呵。

不好意思,不过我只把这里当存一些有点用的地方了,具体说刷分吗,分数我倒不看重,楼主要扣就扣吧;顺便问一句,怎么才能发了之后不会挤掉别人的文章,因为影响质量也不好;
0 请登录后投票
   发表时间:2009-01-15  
你发的这些东西有什么用?怎么用?
0 请登录后投票
   发表时间:2009-01-15  
给个理由为什么不去看官方文档而看你的贴子
0 请登录后投票
   发表时间:2009-02-01  
因为这是中文~~~~
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics