依赖关系

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

方法

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

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

05-11 23:03