许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  Apache HttpClient流式组件fluent-hc:轻量级HTTP客户端

Apache HttpClient流式组件fluent-hc:轻量级HTTP客户端

阅读数 1
点赞 0
article_banner

前言

在使用HttpClient进行java端调用 http  请求时候,发现有流式组件fluent-hc可以直接用,是对HttpClient的简单封装

添加依赖

  <dependency>
            <!--fluent-hc是HttpClient基于流式API封装的客户端-->
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.5.8</version>
  </dependency>

GET请求

Request.Get("http://somehost/")
        .connectTimeout(1000)
        .socketTimeout(1000)
        .execute().returnContent().asString();
  • connectTimeout()设置连接超时
  • socketTimeout() 设置文本读取超时
  • execut() 执行远程连接的核心方法,就是发起一个HttpRequest并返回一个HttpResponse
  • returnContent() 获取返回请求结果Content,其实也就是流文本

POST请求

Request.Post("http://somehost/do-stuff")
        .useExpectContinue()
        .version(HttpVersion.HTTP_1_1)
        .bodyString("{\key\":\"value\"}", ContentType.APPLICATION_JSON)
        //.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
        .execute().returnContent().asString();
  • useExpectContinue() 用户tcp握手
  • version() 指定http传输协议版本(没找到2.0,是不支持还是什么的,还在探究汇中。。。)
  • 请求体body可以使用方法bodyString()按照文本格式传入即可,如:json字符串的文本类型APPLICATION_JSON
  • bodyForm()方法构造表单提交。
  • 根据需要返回不同的文本类型asBytes(),asString(),也支持直接写入文件saveContent()

Executor执行器

验证缓存身份细节并为后续请求重用
Executor executor = Executor.newInstance()
        .auth(new HttpHost("somehost"), "username", "password")
        .auth(new HttpHost("somehost", 8080), "username", "password")
        .authPreemptive(new HttpHost("somehost", 8080));

/**
 * 基于Basic Auth 认证
*/
CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("username", "password"));
executor.use(credsProvider);

executor.execute(Request.Get("http://somehost/"))
        .returnContent().asString();

executor.execute(Request.Post("http://somehost/do-stuff")
        .useExpectContinue()
        .bodyString("{\key\":\"value\"}", ContentType.APPLICATION_JSON)
        .returnContent().asString();

Executor执行器的 CLIENT  对象是从连接池中获取的HttpClient对象。采用HTTP连接池 技术 降低了频繁建立HTTP连接的时间开销,减少了TCP连接建立和释放时socket通信服务器端资源的浪费,同时支持更高的并发量。

public class Executor {
    static final PoolingHttpClientConnectionManager CONNMGR;
    static final HttpClient CLIENT;
    private final HttpClient httpclient;
    private volatile AuthCache authCache;
    private volatile CredentialsProvider credentialsProvider;
    private volatile CookieStore cookieStore;

    public static Executor newInstance() {
        return new Executor(CLIENT);
    }

...

Registry<ConnectionSocketFactory> sfr = RegistryBuilder.create().register("http",     PlainConnectionSocketFactory.getSocketFactory()).register("https", ssl != null ? ssl :  SSLConnectionSocketFactory.getSocketFactory()).build();
        CONNMGR = new PoolingHttpClientConnectionManager(sfr);
        //将每个路由的默认最大连接数设置为100
        CONNMGR.setDefaultMaxPerRoute(100);
        //设置最大连接数
        CONNMGR.setMaxTotal(200);
        CONNMGR.setValidateAfterInactivity(1000);
        CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
}

自定义响应处理

如上所说,execut()执行后得到一个HttpResponse对象,可以 使用方法 handleResponse()自定义响应处理。

避免必须在内存中缓冲内容,提高效率。*

如下就是解析xml格式的示例,默认的方法returnContent()的底层实现其实handleResponse()

Document result = Request.Get("http://somehost/content")
        .execute().handleResponse(new ResponseHandler<Document>() {

    public Document handleResponse(final HttpResponse response) throws IOException {
        StatusLine statusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(
                    statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }
        if (entity == null) {
            throw new ClientProtocolException("Response contains no content");
        }
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            ContentType contentType = ContentType.getOrDefault(entity);
            if (!contentType.equals(ContentType.APPLICATION_XML)) {
                throw new ClientProtocolException("Unexpected content type:" +
                    contentType);
            }
            String charset = contentType.getCharset();
            if (charset == null) {
                charset = HTTP.DEFAULT_CONTENT_CHARSET;
            }
            return docBuilder.parse(entity.getContent(), charset);
        } catch (ParserConfigurationException ex) {
            throw new IllegalStateException(ex);
        } catch (SAXException ex) {
            throw new ClientProtocolException("Malformed XML document", ex);
        }
    }

    });

后记

组件fluent-hc优点:

  1. 链式操作,简单容读
  2. 不必处理连接管理和资源分配
  3. 连接池技术减少资源开销增强性能


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空