少年阿賓

          那些青春的歲月

            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");
               }
           }
          主站蜘蛛池模板: 望城县| 吴堡县| 红桥区| 双桥区| 来宾市| 巨野县| 彭水| 团风县| 卓尼县| 湖口县| 玉门市| 涞水县| 泰来县| 汶上县| 衡山县| 溆浦县| 耒阳市| 益阳市| 阳谷县| 绍兴县| 廊坊市| 安西县| 驻马店市| 广昌县| 西丰县| 吴堡县| 万州区| 乳山市| 扎赉特旗| 承德市| 湘阴县| 克山县| 乐山市| 赣榆县| 六枝特区| 宝应县| 永兴县| 乡城县| 黎城县| 昆山市| 信宜市|