欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

httpclient connect連接請(qǐng)求方法源碼解讀

 更新時(shí)間:2023年11月26日 11:52:43   作者:codecraft  
這篇文章主要為大家介紹了httpclient connect連接請(qǐng)求方法解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下httpclient的connect

HttpClientConnectionOperator

org/apache/http/conn/HttpClientConnectionOperator.java

public interface HttpClientConnectionOperator {

    void connect(
            ManagedHttpClientConnection conn,
            HttpHost host,
            InetSocketAddress localAddress,
            int connectTimeout,
            SocketConfig socketConfig,
            HttpContext context) throws IOException;

    void upgrade(
            ManagedHttpClientConnection conn,
            HttpHost host,
            HttpContext context) throws IOException;

}
HttpClientConnectionOperator定義了connect及upgrade方法,它有一個(gè)默認(rèn)的實(shí)現(xiàn)類為DefaultHttpClientConnectionOperator

DefaultHttpClientConnectionOperator

org/apache/http/impl/conn/DefaultHttpClientConnectionOperator.java

public class DefaultHttpClientConnectionOperator implements HttpClientConnectionOperator {
    static final String SOCKET_FACTORY_REGISTRY = "http.socket-factory-registry";
    private final Log log = LogFactory.getLog(getClass());
    private final Lookup<ConnectionSocketFactory> socketFactoryRegistry;
    private final SchemePortResolver schemePortResolver;
    private final DnsResolver dnsResolver;
    public DefaultHttpClientConnectionOperator(
            final Lookup<ConnectionSocketFactory> socketFactoryRegistry,
            final SchemePortResolver schemePortResolver,
            final DnsResolver dnsResolver) {
        super();
        Args.notNull(socketFactoryRegistry, "Socket factory registry");
        this.socketFactoryRegistry = socketFactoryRegistry;
        this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
            DefaultSchemePortResolver.INSTANCE;
        this.dnsResolver = dnsResolver != null ? dnsResolver :
            SystemDefaultDnsResolver.INSTANCE;
    }
    //......
    public void connect(
            final ManagedHttpClientConnection conn,
            final HttpHost host,
            final InetSocketAddress localAddress,
            final int connectTimeout,
            final SocketConfig socketConfig,
            final HttpContext context) throws IOException {
        final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(context);
        final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName());
        if (sf == null) {
            throw new UnsupportedSchemeException(host.getSchemeName() +
                    " protocol is not supported");
        }
        final InetAddress[] addresses = host.getAddress() != null ?
                new InetAddress[] { host.getAddress() } : this.dnsResolver.resolve(host.getHostName());
        final int port = this.schemePortResolver.resolve(host);
        for (int i = 0; i < addresses.length; i++) {
            final InetAddress address = addresses[i];
            final boolean last = i == addresses.length - 1;
            Socket sock = sf.createSocket(context);
            sock.setSoTimeout(socketConfig.getSoTimeout());
            sock.setReuseAddress(socketConfig.isSoReuseAddress());
            sock.setTcpNoDelay(socketConfig.isTcpNoDelay());
            sock.setKeepAlive(socketConfig.isSoKeepAlive());
            if (socketConfig.getRcvBufSize() > 0) {
                sock.setReceiveBufferSize(socketConfig.getRcvBufSize());
            }
            if (socketConfig.getSndBufSize() > 0) {
                sock.setSendBufferSize(socketConfig.getSndBufSize());
            }
            final int linger = socketConfig.getSoLinger();
            if (linger >= 0) {
                sock.setSoLinger(true, linger);
            }
            conn.bind(sock);
            final InetSocketAddress remoteAddress = new InetSocketAddress(address, port);
            if (this.log.isDebugEnabled()) {
                this.log.debug("Connecting to " + remoteAddress);
            }
            try {
                sock = sf.connectSocket(
                        connectTimeout, sock, host, remoteAddress, localAddress, context);
                conn.bind(sock);
                if (this.log.isDebugEnabled()) {
                    this.log.debug("Connection established " + conn);
                }
                return;
            } catch (final SocketTimeoutException ex) {
                if (last) {
                    throw new ConnectTimeoutException(ex, host, addresses);
                }
            } catch (final ConnectException ex) {
                if (last) {
                    final String msg = ex.getMessage();
                    throw "Connection timed out".equals(msg)
                                    ? new ConnectTimeoutException(ex, host, addresses)
                                    : new HttpHostConnectException(ex, host, addresses);
                }
            } catch (final NoRouteToHostException ex) {
                if (last) {
                    throw ex;
                }
            }
            if (this.log.isDebugEnabled()) {
                this.log.debug("Connect to " + remoteAddress + " timed out. " +
                        "Connection will be retried using another IP address");
            }
        }
    }
}
DefaultHttpClientConnectionOperator的connect先通過(guò)getSocketFactoryRegistry獲取Lookup<ConnectionSocketFactory>,再通過(guò)它獲取ConnectionSocketFactory,之后通過(guò)dnsResolver解析地址,再通過(guò)schemePortResolver解析port,然后通過(guò)ConnectionSocketFactory創(chuàng)建socket,并根據(jù)socketConfig設(shè)置socket的參數(shù),最后執(zhí)行connectSocket,并綁定到conn

connectSocket

org/apache/http/conn/socket/PlainConnectionSocketFactory.java

public class PlainConnectionSocketFactory implements ConnectionSocketFactory {

    public static final PlainConnectionSocketFactory INSTANCE = new PlainConnectionSocketFactory();

    public static PlainConnectionSocketFactory getSocketFactory() {
        return INSTANCE;
    }

    public PlainConnectionSocketFactory() {
        super();
    }

    @Override
    public Socket createSocket(final HttpContext context) throws IOException {
        return new Socket();
    }

    @Override
    public Socket connectSocket(
            final int connectTimeout,
            final Socket socket,
            final HttpHost host,
            final InetSocketAddress remoteAddress,
            final InetSocketAddress localAddress,
            final HttpContext context) throws IOException {
        final Socket sock = socket != null ? socket : createSocket(context);
        if (localAddress != null) {
            sock.bind(localAddress);
        }
        try {
            sock.connect(remoteAddress, connectTimeout);
        } catch (final IOException ex) {
            try {
                sock.close();
            } catch (final IOException ignore) {
            }
            throw ex;
        }
        return sock;
    }

}
PlainConnectionSocketFactory的createSocket直接new一個(gè)socket,其connectSocket方法則執(zhí)行sock.connect

socketConfig

resolveSocketConfig

org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java

private SocketConfig resolveSocketConfig(final HttpHost host) {
        SocketConfig socketConfig = this.configData.getSocketConfig(host);
        if (socketConfig == null) {
            socketConfig = this.configData.getDefaultSocketConfig();
        }
        if (socketConfig == null) {
            socketConfig = SocketConfig.DEFAULT;
        }
        return socketConfig;
    }
PoolingHttpClientConnectionManager的resolveSocketConfig先是從configData根據(jù)指定host獲取socketConfig,若為null則再?gòu)腸onfigData獲取默認(rèn)的socketConfig,若為null則返回默認(rèn)的socketConfig

setSocketConfig

org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java

public void setDefaultSocketConfig(final SocketConfig defaultSocketConfig) {
        this.configData.setDefaultSocketConfig(defaultSocketConfig);
    }

    public void setSocketConfig(final HttpHost host, final SocketConfig socketConfig) {
        this.configData.setSocketConfig(host, socketConfig);
    }
PoolingHttpClientConnectionManager提供了setDefaultSocketConfig、setSocketConfig方法

SocketConfig.DEFAULT

org/apache/http/config/SocketConfig.java

public class SocketConfig implements Cloneable {

    public static final SocketConfig DEFAULT = new Builder().build();

    //......

    public static class Builder {

        private int soTimeout;
        private boolean soReuseAddress;
        private int soLinger;
        private boolean soKeepAlive;
        private boolean tcpNoDelay;
        private int sndBufSize;
        private int rcvBufSize;
        private int backlogSize;

        Builder() {
            this.soLinger = -1;
            this.tcpNoDelay = true;
        }

        //......
    }
}
默認(rèn)的socketConfig,除了tcpNoDelay為true,其他的都為false,然后soLinger為-1

小結(jié)

HttpClientConnectionOperator定義了connect及upgrade方法,它有一個(gè)默認(rèn)的實(shí)現(xiàn)類為DefaultHttpClientConnectionOperator;DefaultHttpClientConnectionOperator的connect先通過(guò)getSocketFactoryRegistry獲取Lookup<ConnectionSocketFactory>,再通過(guò)它獲取ConnectionSocketFactory,之后通過(guò)dnsResolver解析地址,再通過(guò)schemePortResolver解析port,然后通過(guò)ConnectionSocketFactory創(chuàng)建socket,并根據(jù)socketConfig設(shè)置socket的參數(shù),最后執(zhí)行connectSocket,并綁定到conn;默認(rèn)的socketConfig,除了tcpNoDelay為true,其他的都為false,然后soLinger為-1。

以上就是httpclient connect連接請(qǐng)求方法源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于httpclient connect連接請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java實(shí)現(xiàn)Excel轉(zhuǎn)換為圖片

    java實(shí)現(xiàn)Excel轉(zhuǎn)換為圖片

    在實(shí)際開發(fā)過(guò)程中,經(jīng)常會(huì)有這樣的需求,需要將Excel表格或特定區(qū)域轉(zhuǎn)換為圖片,所以小編今天就來(lái)為大家介紹一下如何使用Java將Excel轉(zhuǎn)化為圖片吧
    2023-10-10
  • 親測(cè)SpringBoot參數(shù)傳遞及@RequestBody注解---踩過(guò)的坑及解決

    親測(cè)SpringBoot參數(shù)傳遞及@RequestBody注解---踩過(guò)的坑及解決

    這篇文章主要介紹了親測(cè)SpringBoot參數(shù)傳遞及@RequestBody注解---踩過(guò)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot整合junit與Mybatis流程詳解

    SpringBoot整合junit與Mybatis流程詳解

    這篇文章主要介紹了SpringBoot整合第三方技術(shù),包括整合Junit、整合Mybatis,本文通過(guò)實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 簡(jiǎn)單易懂講解happens-before原則

    簡(jiǎn)單易懂講解happens-before原則

    Java內(nèi)存模型中的happens-before是什么?為什么會(huì)有這東西的存在?一個(gè)新東西肯定是上手先,但是等我們空下來(lái)回過(guò)頭來(lái),我們還是需要去理解這些知識(shí),只有這樣我才能深刻的記住,并且運(yùn)用熟練。下來(lái)和小編來(lái)一起學(xué)習(xí)下
    2019-05-05
  • struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼

    struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼

    struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼,需要的朋友可以參考一下
    2013-04-04
  • Mybatis無(wú)法獲取帶有下劃線前綴的字段的值問(wèn)題

    Mybatis無(wú)法獲取帶有下劃線前綴的字段的值問(wèn)題

    這篇文章主要介紹了Mybatis無(wú)法獲取帶有下劃線前綴的字段的值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題匯總

    Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題匯總

    最近剛學(xué)完MyBatis,趁著大好機(jī)會(huì),總結(jié)一下它的執(zhí)行流程,面試也愛問(wèn)這個(gè),下面這篇文章主要給大家介紹了關(guān)于Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Java簡(jiǎn)單使用EasyExcel操作讀寫excel的步驟與要點(diǎn)

    Java簡(jiǎn)單使用EasyExcel操作讀寫excel的步驟與要點(diǎn)

    相信現(xiàn)在很多搞后端的同學(xué)大部分做的都是后臺(tái)管理系統(tǒng),那么管理系統(tǒng)就肯定免不了Excel的導(dǎo)出導(dǎo)入功能,下面這篇文章主要給大家介紹了關(guān)于Java簡(jiǎn)單使用EasyExcel操作讀寫excel的步驟與要點(diǎn),需要的朋友可以參考下
    2022-09-09
  • java8 stream的分組功能實(shí)例介紹

    java8 stream的分組功能實(shí)例介紹

    這篇文章主要給大家介紹了關(guān)于java8 stream的分組功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 如何動(dòng)態(tài)替換Spring容器中的Bean

    如何動(dòng)態(tài)替換Spring容器中的Bean

    這篇文章主要介紹了如何動(dòng)態(tài)替換Spring容器中的Bean,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論