HttpClient HttpRoutePlanner接口确定请求目标路由

 更新时间:2023年10月16日 09:30:31   作者:codecraft  
这篇文章主要为大家介绍了使用HttpClient HttpRoutePlanner接口确定请求目标路由,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下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方法,用于决定该请求的目标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实现了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0时通过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方法,用于根据协议来返回真正的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

小结

HttpClient的HttpRoutePlanner接口定义了determineRoute方法,用于决定该请求的目标route;

DefaultRoutePlanner实现了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0时通过schemePortResolver.resolve来确定port(http返回80,https返回443)。

以上就是HttpClient HttpRoutePlanner接口确定请求目标路由的详细内容,更多关于HttpClient HttpRoutePlanner接口的资料请关注脚本之家其它相关文章!

相关文章

  • 详解Intellij IDEA 2017 debug断点调试技巧(总结)

    详解Intellij IDEA 2017 debug断点调试技巧(总结)

    这篇文章主要介绍了详解Intellij IDEA 2017 debug断点调试技巧(总结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • 如何基于Jenkins构建Jmeter项目

    如何基于Jenkins构建Jmeter项目

    这篇文章主要介绍了如何基于Jenkins构建Jmeter项目,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Hibernate的一对一,一对多/多对一关联保存的实现

    Hibernate的一对一,一对多/多对一关联保存的实现

    本文主要介绍了Hibernate的一对一,一对多/多对一关联保存的实现,文中通过示例代码介绍的很详细,感兴趣的可以了解一下
    2021-09-09
  • Springboot集成mybatis实现多数据源配置详解流程

    Springboot集成mybatis实现多数据源配置详解流程

    在日常开发中,若遇到多个数据源的需求,怎么办呢?通过springboot集成mybatis实现多数据源配置,简单尝试一下,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 总结Java常用的时间相关转化

    总结Java常用的时间相关转化

    今天给大家带来的是关于Java的相关知识,文章围绕着Java常用的时间相关转化展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • 一文详解Java中的原子操作

    一文详解Java中的原子操作

    在Java中,原子操作尤为重要,尤其是在多线程环境中,想象一下,如果小黑在操作一个共享变量时,这个操作被其他线程打断,那会发生什么?可能会导致数据不一致,或者更糟糕的情况,本文将给大家详细介绍一下Java中的原子操作
    2024-01-01
  • java创建jar包并被项目引用步骤详解

    java创建jar包并被项目引用步骤详解

    这篇文章主要介绍了java创建jar包并被项目引用步骤详解,jar包实现了特定功能的,java字节码文件的压缩包,更多相关内容需要的朋友可以参考一下
    2022-07-07
  • Java DelayQueue实现任务延时示例讲解

    Java DelayQueue实现任务延时示例讲解

    DelayQueue是一个无界的BlockingQueue的实现类,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。本文就来利用DelayQueue实现延时任务,感兴趣的可以了解一下
    2022-09-09
  • SpringBoot JPA使用配置过程详解

    SpringBoot JPA使用配置过程详解

    这篇文章主要介绍了SpringBoot JPA使用配置过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Java ArrayList扩容问题实例详解

    Java ArrayList扩容问题实例详解

    这篇文章主要介绍了Java ArrayList扩容问题实例详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02

最新评论