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

HttpClient学习(二)—— MinimalHttpClient源码解析

时间:2019-05-13 14:22:10      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:nts   rri   detail   apache   基础配置   rgs   视频   sync   包装类   

依赖关系

技术图片

方法

技术图片

class MinimalHttpClient extends CloseableHttpClient {
    
    //连接管理器
    private final HttpClientConnectionManager connManager;
    //执行器
    private final MinimalClientExec requestExecutor;
    //参数
    private final HttpParams params;
}

doExecute方法



 @Override
    protected CloseableHttpResponse doExecute(
            final HttpHost target,
            final HttpRequest request,
            final HttpContext context) throws IOException, ClientProtocolException {
        Args.notNull(target, "Target host");
        Args.notNull(request, "HTTP request");
        //Interface to be implemented by any object 
        //that wishes to be notified of blocking I/O operations 
        //that could be cancelled.
        //blocking I/O .....
        HttpExecutionAware execAware = null;
        if (request instanceof HttpExecutionAware) {
            execAware = (HttpExecutionAware) request;
        }
        try {
            //HttpRequest的包装类
            final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
            final HttpClientContext localcontext = HttpClientContext.adapt(
                context != null ? context : new BasicHttpContext());
            //指明服务器的地址
            final HttpRoute route = new HttpRoute(target);
            //请求相关的基础配置
            RequestConfig config = null;
            if (request instanceof Configurable) {
                config = ((Configurable) request).getConfig();
            }
            if (config != null) {
                localcontext.setRequestConfig(config);
            }
            return this.requestExecutor.execute(route, wrapper, localcontext, execAware);
        } catch (final HttpException httpException) {
            throw new ClientProtocolException(httpException);
        }
    }

最后请求会交给MinimalClientExec 对象处理。

MinimalClientExec




基础回顾

Blocking I/O

JAVA I/O分为BIO(Blocking IO)、NIO(Non-blocking IO)和AIO(Asynchronous IO)。这里要讲到的就是Blocking I/O。

参考文档

HttpClient源码解析系列:第二篇:极简版实现
HttpClient官网 Quick Start

参考视频

一头扎进HttpClient

HttpClient学习(二)—— MinimalHttpClient源码解析

标签:nts   rri   detail   apache   基础配置   rgs   视频   sync   包装类   

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

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