httpclient -- InternalHttpClient解析

来源:互联网 发布:纹身贴纸淘宝 编辑:程序博客网 时间:2024/06/08 03:26

1.doExecute(final HttpHost target,
final HttpRequest request,
final HttpContext context)
主要步骤:
1.

final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request, target);

将request和target进行封装。
2.

            final HttpClientContext localcontext = HttpClientContext.adapt(                    context != null ? context : new BasicHttpContext());

判断参数context是否为空,如果为空,那么就新建一个httpclientContext。
3.

   RequestConfig config = null;            if (request instanceof Configurable) {                config = ((Configurable) request).getConfig();            }            if (config == null) {                final HttpParams params = request.getParams();                if (params instanceof HttpParamsNames) {                    if (!((HttpParamsNames) params).getNames().isEmpty()) {                        config = HttpClientParamConfig.getRequestConfig(params, this.defaultConfig);                    }                } else {                    config = HttpClientParamConfig.getRequestConfig(params, this.defaultConfig);                }            }

判断request中是否有设置了config(比如连接时间限制等等),如果人工设置了,将其抽出,同时为了和旧版本兼容,也会检查request是否设置了params,如果有的话,就将它转成config,并抽出,如果仍然没有,那么InternalHttpClient就会用其final域中的defaultConfig的替代。
4.

   if (config != null) {                localcontext.setRequestConfig(config);            }

之后,就将config信息存储到localcontext中
5.

 setupContext(localcontext);

设置localcontext的相关参数。setupContext的相关代码如下

    private void setupContext(final HttpClientContext context) {        if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {            context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState());        }        if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {            context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState());        }        if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {            context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);        }        if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {            context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);        }        if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {            context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);        }        if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {            context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);        }        if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {            context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);        }    }

6.

 final HttpRoute route = determineRoute(target, wrapper, localcontext);

确定路由。
7.

this.execChain.execute(route, wrapper, localcontext, execAware);

调用execChain的execute方法。

原创粉丝点击