HttpClient学习—— MinimalHttpClient源码解析

Posted fonxian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient学习—— MinimalHttpClient源码解析相关的知识,希望对你有一定的参考价值。

依赖关系

技术图片

方法

技术图片

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源码解析的主要内容,如果未能解决你的问题,请参考以下文章

httpclient 学习

HttpClient使用学习

HttpClient学习--HttpClient的POST请求过程源码解读

HttpClient学习整理

HttpClient学习整理

httpclient学习