HttpClient源码解析

变量

//http连接管理器
private HttpConnectionManager httpConnectionManager;
//http状态
private HttpState state;
//http客户端参数
private HttpClientParams params;
//主机配置
private HostConfiguration hostConfiguration;

对于以上变量的赋值部位分别在Httpclient初始化阶段,execute执行阶段

http连接管理器(httpConnectionManager)和客户端参数(params)在Httpclient初始化阶段被赋值的。http状态(state)和主机配置(hostConfiguration)在执行execute阶段被赋值的。而这些变量中的内容Httpclient也提供了大量的get,set方法对其进行修改

 

 

构造方法

使用默认参数集创建httpclient的实例

public HttpClient() {
    //创建一个空的参数集并调用给定参数集的构造器
    this((HttpClientParams)(new HttpClientParams()));
}

 

使用给定的参数集创建httpclient的实例

public HttpClient(HttpClientParams params) {
    //初始化状态类
    this.state = new HttpState();
    //初始化参数
    this.params = null;
    //初始化主机配置参数
    this.hostConfiguration = new HostConfiguration();
    if(params == null) {
        throw new IllegalArgumentException("Params may not be null");
    } else {
        //如果传入的参数不为空则将其赋值给httpcient的params
        this.params = params;
        //初始化http连接管理器
        this.httpConnectionManager = null;
        //获取传入参数中调用方指定的连接管理器
        Class clazz = params.getConnectionManagerClass();
        if(clazz != null) {
            //如果调用方指定了连接管理器执行以下代码
            try {
                //将调用方指定的连接的连接管理器强转成HttpConnectionManager类型
                this.httpConnectionManager = (HttpConnectionManager)clazz.newInstance();
            } catch (Exception var4) {
                LOG.warn("Error instantiating connection manager class, defaulting to SimpleHttpConnectionManager", var4);
            }
        }
        //如果调用方没有指定连接器,则默认使用SimpleHttpConnectionManager连接器
        if(this.httpConnectionManager == null) {
            this.httpConnectionManager = new SimpleHttpConnectionManager();
        }
        //如果连接器不为空则将传入的参数集设置成连接器的(实际这段代码中httpConnectionManager一定不为空,不过为了保险期间还是做了个判断)
        if(this.httpConnectionManager != null) {
            this.httpConnectionManager.getParams().setDefaults(this.params);
        }
    }
}

 

使用用户指定的参数集和HTTP连接管理器创建httpclient实例

public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) {
    //初始化状态类
    this.state = new HttpState();
    //初始化参数
    this.params = null;
    //初始化主机配置参数
    this.hostConfiguration = new HostConfiguration();
    //检验参数,为空则抛异常
    if(httpConnectionManager == null) {
        throw new IllegalArgumentException("httpConnectionManager cannot be null");
    } else if(params == null) {
        throw new IllegalArgumentException("Params may not be null");
    } else {
        //参数赋值
        this.params = params;
        this.httpConnectionManager = httpConnectionManager;
        this.httpConnectionManager.getParams().setDefaults(this.params);
    }
}

 

使用用户指定的HTTP连接管理器创建httpclient实例

public HttpClient(HttpConnectionManager httpConnectionManager) {
    this(new HttpClientParams(), httpConnectionManager);
}

 

主要方法execute

执行给定的HTTP方法,其他参数使用默认值

public int executeMethod(HttpMethod method) throws IOException, HttpException {
    LOG.trace("enter HttpClient.executeMethod(HttpMethod)");
    return this.executeMethod((HostConfiguration)null, method, (HttpState)null);
}

 

使用自定义主机配置执行给定的HTTP方法。

public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method) throws IOException, HttpException {
    LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)");
    return this.executeMethod(hostConfiguration, method, (HttpState)null);
}

 

使用具有给定自定义HTTP状态的给定自定义主机配置执行给定的HTTP方法。

public int executeMethod(HostConfiguration hostconfig, HttpMethod method, HttpState state) throws IOException, HttpException {
    LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)");
    if(method == null) {
        throw new IllegalArgumentException("HttpMethod parameter may not be null");
    } else {
        //获取httpclient默认的hostconfig,在httpclient初始化时对hostconfig的处理是直接new一个HostConfiguration(),然后并没有进行过多的处理
        HostConfiguration defaulthostconfig = this.getHostConfiguration();
        if(hostconfig == null) {
            //如果传参的hostconfig为空,则将httpclient默认的主机配置赋值给hostconfig
            hostconfig = defaulthostconfig;
        }
        //获取Http方法中的URI信息
        URI uri = method.getURI();
        if(hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
            //如果传入的主机配置和默认配置相等或uri为绝对路径则对hostconfig进行浅克隆
            hostconfig = (HostConfiguration)hostconfig.clone();
            if(uri.isAbsoluteURI()) {
                //如果uri为绝对路径则将次uri设置为主机host
                hostconfig.setHost(uri);
            }
        }
        //通过httpclient的四个参数创建一个HTTP方法控制器
        HttpMethodDirector methodDirector = new HttpMethodDirector(this.getHttpConnectionManager(), hostconfig, this.params, state == null?this.getState():state);
        //根据httpclient创建的HTTP方法控制器去执行 HTTP方法
        methodDirector.executeMethod(method);
        //返回执行完的HTTP方法的状态码
        return method.getStatusCode();
    }
}

可以看到execute方法最后的结果是由HttpMethodDirector类执行的,接下来解析HttpMethodDirector类的源码:https://blog.csdn.net/dancheng1/article/details/88397079

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值