码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient学习(三)—— AsyncHttpClient使用

时间:2019-05-21 20:57:52      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:.exe   group   依赖   lock   dex   compiler   同步请求   mave   exec   

一、介绍

This class support asynchronous and synchronous HTTP requests.

AsyncHttpClient 支持同步、异步Http请求。

二、简单使用

引入依赖


<dependencies>

        <dependency>
            <groupId>org.asynchttpclient</groupId>
            <artifactId>async-http-client</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>net.tascalate</groupId>
            <artifactId>net.tascalate.concurrent</artifactId>
            <version>0.8.0</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

执行同步请求


/**
     * 执行同步HTTP请求
     */
    public void synRequest() {

        String url = "http://www.baidu.com";
        AsyncHttpClient c = new DefaultAsyncHttpClient();
        Future<Response> f = c.prepareGet(url).execute();
        try {
            System.out.println(f.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

执行异步请求

参考文档

《AsyncHttpClient 官方文档》

HttpClient学习(三)—— AsyncHttpClient使用

标签:.exe   group   依赖   lock   dex   compiler   同步请求   mave   exec   

原文地址:https://www.cnblogs.com/fonxian/p/10902172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!