HttpClient HttpRoutePlanner接口確定請求目標(biāo)路由
序
本文主要研究一下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)文章!
- httpclient connect連接請求方法源碼解讀
- httpclient getPoolEntryBlocking連接池方法源碼解讀
- httpclient staleConnectionCheckEnabled獲取連接流程解析
- 解讀httpclient的validateAfterInactivity連接池狀態(tài)檢測
- httpclient的disableConnectionState方法工作流程
- 探索HttpClient中的close方法及其對連接的影響
- HttpClient的RedirectStrategy重定向處理核心機(jī)制
- httpclient ConnectionHolder連接池連接保持源碼解析
相關(guā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ū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java中的Opencv簡介與開發(fā)環(huán)境部署方法
OpenCV是一個(gè)開源的計(jì)算機(jī)視覺和圖像處理庫,提供了豐富的圖像處理算法和工具,它支持多種圖像處理和計(jì)算機(jī)視覺算法,可以用于物體識別與跟蹤、圖像分割與邊緣檢測、圖像特征提取與描述等應(yīng)用,本文介紹Java中的Opencv簡介與開發(fā)環(huán)境部署方法,感興趣的朋友一起看看吧2025-01-01Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)
本篇文章給大家詳細(xì)講述了Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)的方法,以及代碼分享,有興趣的參考學(xué)習(xí)下。2018-02-02Java?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