国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現HttpClient異步請求資源的方法

2019-11-26 15:05:27
字體:
來源:轉載
供稿:網友

本文實例講述了java實現HttpClient異步請求資源的方法。分享給大家供大家參考。具體實現方法如下:

package demo;import java.util.concurrent.CountDownLatch;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;import org.apache.http.nio.client.HttpAsyncClient;import org.apache.http.nio.concurrent.FutureCallback;import org.apache.http.nio.reactor.IOReactorException;public class Main { /**  * @param args  * @throws IOReactorException   * @throws InterruptedException   */ public static void main(String[] args) throws IOReactorException, InterruptedException {  final HttpAsyncClient httpclient = new DefaultHttpAsyncClient();  httpclient.start();  HttpGet[] requests = new HttpGet[] {    new HttpGet("http://www.apache.org/"),    new HttpGet("https://www.verisign.com/"),    new HttpGet("http://www.google.com/")  };  final CountDownLatch latch = new CountDownLatch(requests.length);  try {   for (final HttpGet request: requests) {    httpclient.execute(request, new FutureCallback<HttpResponse>() {     public void completed(final HttpResponse response) {      latch.countDown();      System.out.println(request.getRequestLine() + "->" + response.getStatusLine());     }     public void failed(final Exception ex) {      latch.countDown();      ex.printStackTrace();     }     public void cancelled() {      latch.countDown();     }    });   }   System.out.println("Doing...");  }finally {   latch.await();   httpclient.shutdown();  }  System.out.println("Done"); }}

希望本文所述對大家的java程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭泽县| 尤溪县| 鹿泉市| 虞城县| 台中市| 新源县| 永州市| 菏泽市| 独山县| 儋州市| 福建省| 肇源县| 土默特左旗| 夹江县| 三台县| 大丰市| 读书| 中山市| 宁阳县| 兴化市| 桃江县| 金华市| 潮安县| 梧州市| 屏山县| 乐业县| 株洲县| 永川市| 临邑县| 东丰县| 格尔木市| 赞皇县| 湖北省| 永吉县| 武宣县| 金塔县| 河间市| 榆树市| 浮梁县| 明溪县| 磴口县|