美文网首页
HttpAsyncClient

HttpAsyncClient

作者: wutianf | 来源:发表于2019-01-29 00:07 被阅读0次
package com.wutianfa.experience;

import org.apache.http.HttpEntity;
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.Args;
import org.junit.Test;

import java.io.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Samuel on 2019/1/28 21:16
 */
public class ChunkTest {
    private static final String uri = "https://vscode.cdn.azure.cn/stable/61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8" +
            "/VSCode-win32-x64-1.30.2.zip";
    private static String path = "E:\\test.zip";


    @Test
    public void test() {

        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        httpClient.start();
        final CountDownLatch latch = new CountDownLatch(1);
        final HttpGet request = new HttpGet(uri);
        //设置代理
        //HttpHost proxy = new HttpHost("10.10.10.10", 8080, "http");
        //RequestConfig requestConfig = RequestConfig.custom()
        //        .setProxy(proxy).build();
        //request.setConfig(requestConfig);
        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());
                BufferedOutputStream bout = null;
                OutputStream outputStream = null;
                InputStream isstream = null;
                try {
                    HttpEntity entity = response.getEntity();
                    Args.notNull(entity, "Entity");
                    isstream = entity.getContent();
                    Args.notNull(isstream, "isstream");
                    Args.check(entity.getContentLength() <= 2147483647L, "HTTP entity too large to be " +
                            "buffered in memory");
                    byte[] tmp = new byte[4096];
                    outputStream = new FileOutputStream(path);
                    bout = new BufferedOutputStream(outputStream);
                    int l;
                    while ((l = isstream.read(tmp)) != -1) {
                        bout.write(tmp, 0, l);
                        bout.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        assert bout != null;
                        bout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        outputStream.close();
                        isstream.close();
                    } 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();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

相关文章

网友评论

      本文标题:HttpAsyncClient

      本文链接:https://www.haomeiwen.com/subject/onltsqtx.html