Http异步client

来源:互联网 发布:活动报名网站源码 编辑:程序博客网 时间:2024/06/01 20:53
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.CloseableHttpAsyncClient;import org.apache.http.impl.nio.client.HttpAsyncClients;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.util.concurrent.CountDownLatch;public class Main {    public static void main(String[] argv) {        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();        httpclient.start();        final CountDownLatch latch = new CountDownLatch(1);        final HttpGet request = new HttpGet("https://www.alipay.com/");        System.out.println(" caller thread id is : " + Thread.currentThread().getId());        httpclient.execute(request, new FutureCallback<HttpResponse>() {            public void completed(final HttpResponse response) {                latch.countDown();                System.out.println(" callback thread id is : " + Thread.currentThread().getId());                System.out.println(request.getRequestLine() + "->" + response.getStatusLine());                try {                    String content = EntityUtils.toString(response.getEntity(), "UTF-8");                    System.out.println(" response content is : " + content);                } catch (IOException e) {                    e.printStackTrace();                }            }            public void failed(final Exception ex) {                latch.countDown();                System.out.println(request.getRequestLine() + "->" + ex);                System.out.println(" callback thread id is : " + Thread.currentThread().getId());            }            public void cancelled() {                latch.countDown();                System.out.println(request.getRequestLine() + " cancelled");                System.out.println(" callback thread id is : " + Thread.currentThread().getId());            }        });        try {            latch.await();        } catch (InterruptedException e) {            e.printStackTrace();        }        try {            httpclient.close();        } catch (IOException ignore) {        }    }}

http://www.tuicool.com/articles/bi226f
http://www.cnblogs.com/guogangj/p/5457959.html

0 0
原创粉丝点击