- 浏览: 32444 次
- 性别:
- 来自: 湖南
文章分类
最新评论
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); } } } }
有技术兴趣的 请加28830308群.
发表评论
-
HttpClient容易忽视的细节——连接关闭
2012-03-02 09:15 581HttpClient client = new HttpCli ... -
HTTPClient的多线程编程
2012-03-02 09:09 1444Axis2的连接主要采用了HttpClient进行与服务器的服 ... -
HttpClient超时区别
2012-03-02 09:07 1042HttpClient 4 设置超时 httpclient 超时 ... -
HttpClient Theading
2012-03-01 10:49 638有技术兴趣的 请加28830308群. 这篇文章概括了怎样在 ... -
HttpClient使用
2012-03-01 10:43 671有技术兴趣的 请加28830308群. HttpClien ... -
httpclient 4 下载 文件
2012-03-01 10:38 887import java.io.File; import ja ... -
使用了httpclient实现的上传商品的demo
2012-03-01 10:36 1117package com.taobao.top.sample.u ... -
socket简介
2012-02-07 10:58 651第一步 充分理解Socket ... -
转载socket
2012-02-07 10:57 530对TCP/IP、UDP、Socket编程这些词你不会很陌生吧? ... -
Java的synchronized关键字:同步机制总结
2012-02-07 10:54 535不久前用到了同步,现在回过头来对JAVA中的同步做个总结,以对 ... -
JAVA RMI
2012-02-07 10:53 545Java RMI 指的是远程方法调用 (Remote Meth ... -
JXL生成复杂的EXCEL
2011-12-26 13:52 1343public String ExcelTaskRepo ... -
如何快速的为现有数据库建立数据字典?
2011-12-20 13:55 955大部分项目在验收时都需要向客户提供一份详细的数据字典,而编写数 ... -
在java中获取客户端真实的IP地址
2011-12-20 13:55 522public static String getRemoteA ... -
用JavaMail的API发送邮件
2011-12-20 13:55 5001、MimeMessage的包装类 public c ... -
在项目中整合FreeMarker框架
2011-12-16 15:40 558FreeMarker是一个用Java编写的模板引擎,既可以 ... -
Commons FTP范例
2011-12-16 15:33 480public class FtpUtil { privat ... -
Java实现的图片生成器
2011-12-16 15:32 485一、本图片生成器具有以下功能特性: 1、可以设置图 ... -
jacob学习总结
2011-12-16 15:27 402JACOB 就是 JAVA-COM Bridge的缩写,提供自 ... -
长短信发送范例
2011-12-16 15:25 659一、关键代码: private synchronized v ...
相关推荐
基于gdal的影像读写,读写采用分块读写的方式,添加了进度条来显示运行进度,可以支持海量数据的tif影像,程序采用了多线程的方式来提高速度,运行过程中可以随意拖动对话框。
Java多线程程经典案例.doc
很小的程序,运行程序时开子线程运算Fibonacci序列,父线程输出
1.创建多线程程序 2.使用RandomAccessFile类分段读写大文件 3.通过多线程分段读取大文件并分段写入到新文件
多线程查找CRapidFinder.rar 多线程程实现 可以停止线程 暂停线程 结束线程 还可以设置线程优先级(值得学习)
第二套:c语言Socket编程C语言SOCKET编程指南.c语言多进程多线程程.pdf,Linux Socket rogramming by Example.pdf,Linux 多线程等10多个资料....
Delegate 方式的异步线程 学习Dome,其中有四种方式的代码。
湖北多线程程查余额辅助v2.0很暴力,亲测有效全网首发
这是本程程源码,请下载直接修改
这是基于c++ 语言的课程设计,是一个基于数据库的学生选课系统。
(1)利用python多进程程或多线程实时读取远程IP摄像头视频 (2)Pyhon cv2.VideoWriter 保存视频 整合成如下代码,可以对多路网络摄像头图像实时采集并分别保存视频。或者自己改写代码,直接保存每路的实时图像也...
程程电子 单片机 CCDZ-C原理图 欢迎大家使用,网上已经很少了,分享快乐。
多态及封装吗,查询,面向对象,抽象类,子类,父类
学习scala并发的可以下载,需要一定scala基础 学习scala需要看scala编程或者scala编程思想
丁程程述职报告.docx
进程是一个具有独立功能的程序,关于某个特定数据集合进行的一次运行活动。 进程是系统进行资源分配和调度运行的基本单位(程序 + 数据 + 进程控制块PCB),进程会独占CPU。 线程是进程基础上的进一步划分,可...
电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序电梯模拟程序
多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、队列(Queue)、事件(Event)、管道(Pipe) ,并辅之(共享内存、Manager) 不同计算机之间实现生产者消费者模型:socket、远程调用(RPC...
操作系统课程设计-进程程调度和页面转换两个实验,用C++ MFC实现的,可运行。
WINDOWS系统自带画图工具教程程.pdf