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

HttpClient HttpRoutePlanner接口確定請求目標(biāo)路由

 更新時(shí)間:2023年10月16日 09:30:31   作者:codecraft  
這篇文章主要為大家介紹了使用HttpClient HttpRoutePlanner接口確定請求目標(biāo)路由,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下HttpClient的HttpRoutePlanner

HttpRoutePlanner

org/apache/http/conn/routing/HttpRoutePlanner.java

/**
 * Encapsulates logic to compute a {@link HttpRoute} to a target host.
 * Implementations may for example be based on parameters, or on the
 * standard Java system properties.
 * <p>
 * Implementations of this interface must be thread-safe. Access to shared
 * data must be synchronized as methods of this interface may be executed
 * from multiple threads.
 * </p>
 *
 * @since 4.0
 */
public interface HttpRoutePlanner {

    /**
     * Determines the route for a request.
     *
     * @param target    the target host for the request.
     *                  Implementations may accept {@code null}
     *                  if they can still determine a route, for example
     *                  to a default target or by inspecting the request.
     * @param request   the request to execute
     * @param context   the context to use for the subsequent execution.
     *                  Implementations may accept {@code null}.
     *
     * @return  the route that the request should take
     *
     * @throws HttpException    in case of a problem
     */
    HttpRoute determineRoute(HttpHost target,
                                    HttpRequest request,
                                    HttpContext context) throws HttpException;

}
HttpRoutePlanner接口定義了determineRoute方法,用于決定該請求的目標(biāo)route

DefaultRoutePlanner

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

/**
 * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
 * any Java system properties, nor of system or browser proxy settings.
 *
 * @since 4.3
 */
@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
public class DefaultRoutePlanner implements HttpRoutePlanner {
    private final SchemePortResolver schemePortResolver;
    public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
        super();
        this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
            DefaultSchemePortResolver.INSTANCE;
    }
    @Override
    public HttpRoute determineRoute(
            final HttpHost host,
            final HttpRequest request,
            final HttpContext context) throws HttpException {
        Args.notNull(request, "Request");
        if (host == null) {
            throw new ProtocolException("Target host is not specified");
        }
        final HttpClientContext clientContext = HttpClientContext.adapt(context);
        final RequestConfig config = clientContext.getRequestConfig();
        final InetAddress local = config.getLocalAddress();
        HttpHost proxy = config.getProxy();
        if (proxy == null) {
            proxy = determineProxy(host, request, context);
        }
        final HttpHost target;
        if (host.getPort() <= 0) {
            try {
                target = new HttpHost(
                        host.getHostName(),
                        this.schemePortResolver.resolve(host),
                        host.getSchemeName());
            } catch (final UnsupportedSchemeException ex) {
                throw new HttpException(ex.getMessage());
            }
        } else {
            target = host;
        }
        final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
        return proxy == null
                        ? new HttpRoute(target, local, secure)
                        : new HttpRoute(target, local, proxy, secure);
    }
    /**
     * This implementation returns null.
     *
     * @throws HttpException may be thrown if overridden
     */
    protected HttpHost determineProxy(
            final HttpHost target,
            final HttpRequest request,
            final HttpContext context) throws HttpException {
        return null;
    }
}
DefaultRoutePlanner實(shí)現(xiàn)了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0時(shí)通過schemePortResolver.resolve來確定port

SchemePortResolver

org/apache/http/conn/SchemePortResolver.java

/**
 * Strategy for default port resolution for protocol schemes.
 *
 * @since 4.3
 */
public interface SchemePortResolver {
    /**
     * Returns the actual port for the host based on the protocol scheme.
     */
    int resolve(HttpHost host) throws UnsupportedSchemeException;
}
SchemePortResolver接口定義了resolve方法,用于根據(jù)協(xié)議來返回真正的port

DefaultSchemePortResolver

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

@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class DefaultSchemePortResolver implements SchemePortResolver {
    public static final DefaultSchemePortResolver INSTANCE = new DefaultSchemePortResolver();
    @Override
    public int resolve(final HttpHost host) throws UnsupportedSchemeException {
        Args.notNull(host, "HTTP host");
        final int port = host.getPort();
        if (port > 0) {
            return port;
        }
        final String name = host.getSchemeName();
        if (name.equalsIgnoreCase("http")) {
            return 80;
        } else if (name.equalsIgnoreCase("https")) {
            return 443;
        } else {
            throw new UnsupportedSchemeException(name + " protocol is not supported");
        }
    }
}
DefaultSchemePortResolver方法針對http返回80,針對https返回443,其他的拋出UnsupportedSchemeException

小結(jié)

HttpClient的HttpRoutePlanner接口定義了determineRoute方法,用于決定該請求的目標(biāo)route;

DefaultRoutePlanner實(shí)現(xiàn)了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0時(shí)通過schemePortResolver.resolve來確定port(http返回80,https返回443)。

以上就是HttpClient HttpRoutePlanner接口確定請求目標(biāo)路由的詳細(xì)內(nèi)容,更多關(guān)于HttpClient HttpRoutePlanner接口的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    Spring項(xiàng)目中經(jīng)常需要配置日期時(shí)間格式格式,本文主要介紹了SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • @Bean注解和@Configuration、@Component注解組合使用的區(qū)別

    @Bean注解和@Configuration、@Component注解組合使用的區(qū)別

    這篇文章主要介紹了@Bean注解和@Configuration、@Component注解組合使用的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中的Opencv簡介與開發(fā)環(huán)境部署方法

    Java中的Opencv簡介與開發(fā)環(huán)境部署方法

    OpenCV是一個(gè)開源的計(jì)算機(jī)視覺和圖像處理庫,提供了豐富的圖像處理算法和工具,它支持多種圖像處理和計(jì)算機(jī)視覺算法,可以用于物體識別與跟蹤、圖像分割與邊緣檢測、圖像特征提取與描述等應(yīng)用,本文介紹Java中的Opencv簡介與開發(fā)環(huán)境部署方法,感興趣的朋友一起看看吧
    2025-01-01
  • J2ME編程中的幾個(gè)重要概念介紹

    J2ME編程中的幾個(gè)重要概念介紹

    本文介紹的是J2ME編程應(yīng)用平臺中的幾個(gè)重要概念,希望對你有幫助,一起來看。
    2015-09-09
  • Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)

    Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)

    本篇文章給大家詳細(xì)講述了Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)的方法,以及代碼分享,有興趣的參考學(xué)習(xí)下。
    2018-02-02
  • Java 運(yùn)算符詳情

    Java 運(yùn)算符詳情

    這篇文章主要介紹了Java 運(yùn)算符,Java 中的運(yùn)算符與 C 語言基本一致。下面文章就圍繞Java 中的運(yùn)算符的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • Java 自定義注解及利用反射讀取注解的實(shí)例

    Java 自定義注解及利用反射讀取注解的實(shí)例

    下面小編就為大家?guī)硪黄狫ava 自定義注解及利用反射讀取注解的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Spring?Bean是如何初始化的詳解

    Spring?Bean是如何初始化的詳解

    Spring只Bean加載機(jī)制默認(rèn)情況下是初始化容器的時(shí)候就會(huì)直接初始化,但是也取決于是否使用了懶加載,下面這篇文章主要給大家介紹了關(guān)于Spring?Bean是如何初始化的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Java?8?Stream?處理數(shù)據(jù)方法匯總

    Java?8?Stream?處理數(shù)據(jù)方法匯總

    這篇文章主要介紹了Java?8?Stream處理數(shù)據(jù),Stream是Java?8?新引入的一個(gè)包它讓我們能用聲明式的方式處理數(shù)據(jù),Stream流式處理相較于傳統(tǒng)方法簡潔高效,也便于進(jìn)行并發(fā)編程,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • Springboot事務(wù)失效的幾種情況解讀

    Springboot事務(wù)失效的幾種情況解讀

    這篇文章主要介紹了Springboot事務(wù)失效的幾種情況解讀,因?yàn)镾pring AOP默認(rèn)使用動(dòng)態(tài)代理,會(huì)給被代理的類生成一個(gè)代理類,事務(wù)相關(guān)的操作都通過代理來完成,使用內(nèi)部方法調(diào)用時(shí),使用的是實(shí)例調(diào)用,沒有通過代理類調(diào)用方法,因此事務(wù)不會(huì)檢測到失敗,需要的朋友可以參考下
    2023-10-10

最新評論