少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          View Code
           package generate.httpclient;
           
           import java.io.ByteArrayOutputStream;
           import java.io.IOException;
           import java.io.InputStream;
           import java.util.ArrayList;
           import java.util.HashMap;
           import java.util.List;
           import java.util.Map;
           import java.util.concurrent.CountDownLatch;
           
           import org.apache.http.HttpResponse;
           import org.apache.http.client.methods.HttpGet;
           import org.apache.http.concurrent.FutureCallback;
           import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
           import org.apache.http.nio.client.HttpAsyncClient;
           import org.apache.http.nio.reactor.IOReactorException;
           
           public class AsynClient{
               /**
                * @param args
                * @throws IOReactorException
                * @throws InterruptedException
                */
               private List<String> urls;
               private HandlerFailThread failHandler;
               public AsynClient(List<String> list){
                   failHandler=new HandlerFailThread();
                   urls=list;
               }
               public Map<String,String> asynGet() throws IOReactorException,
                       InterruptedException {
                   final HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
                   httpclient.start();
                   int urlLength=urls.size();
                   HttpGet[] requests = new HttpGet[urlLength];
                   int i=0;
                   for(String url : urls){
                       requests[i]=new HttpGet(url);
                       i++;
                   }
                   final CountDownLatch latch = new CountDownLatch(requests.length);
                   final Map<String, String> responseMap=new HashMap<String, String>();
                   try {
                       for (final HttpGet request : requests) {
                           httpclient.execute(request, new FutureCallback<HttpResponse>() {
           
                               public void completed(final HttpResponse response) {
                                   latch.countDown();
                                   responseMap.put(request.getURI().toString(), response.getStatusLine().toString());
                                   try {
                                       System.out.println(request.getRequestLine() + "->"
                                               + response.getStatusLine()+"->");
                                       //+readInputStream(response.getEntity().getContent())
                                      
                                   } catch (IllegalStateException e) {
                                       failHandler.putFailUrl(request.getURI().toString(),
                                               response.getStatusLine().toString());
                                       e.printStackTrace();
                                   } catch (Exception e) {
                                       failHandler.putFailUrl(request.getURI().toString(),
                                               response.getStatusLine().toString());
                                       e.printStackTrace();
                                   }
                               }
           
                               public void failed(final Exception ex) {
                                   latch.countDown();
                                   ex.printStackTrace();
                                   failHandler.putFailUrl(request.getURI().toString(),
                                           ex.getMessage());
                               }
           
                               public void cancelled() {
                                   latch.countDown();
                               }
           
                           });
                       }
                       System.out.println("Doing...");
                   } finally {
                       latch.await();
                       httpclient.shutdown();
                   }
                   System.out.println("Done");
                   failHandler.printFailUrl();
                   return responseMap;
               }
               private String readInputStream(InputStream input) throws IOException{
                   byte[] buffer = new byte[128];
                   int len = 0;
                   ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                   while((len = input.read(buffer)) >= 0) {
                       bytes.write(buffer, 0, len);
                   }
                   return bytes.toString();
               }
               /**
                * Test
                * @param args
                */
               public static void main(String[] args) {
                   List<String> urls=new ArrayList<String>();
                   urls.add("         urls.add("         urls.add("         for(int i=0;i<10;i++){
                       urls.addAll(urls);
                   }
                   System.out.println(urls.size());
                   AsynClient client=new AsynClient(urls);
                   try {
                       client.asynGet();
                   } catch (IOReactorException e) {
                       e.printStackTrace();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println("done");
               }
           }
          主站蜘蛛池模板: 闽清县| 仁化县| 兰州市| 黄大仙区| 延安市| 徐闻县| 富蕴县| 花莲市| 龙海市| 萍乡市| 嵊泗县| 新泰市| 资溪县| 日照市| 西乌珠穆沁旗| 通海县| 东台市| 香河县| 临夏市| 大方县| 灌南县| 高碑店市| 镇远县| 安义县| 青铜峡市| 昌宁县| 泰州市| 贺州市| 河西区| 佛山市| 榆林市| 克山县| 孟津县| 巴南区| 星座| 乳源| 天津市| 闻喜县| 宁德市| 仙居县| 乌拉特前旗|