HttpClient HttpRoutePlanner接口確定請(qǐng)求目標(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方法,用于決定該請(qǐng)求的目標(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í)通過(guò)schemePortResolver.resolve來(lái)確定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é)議來(lái)返回真正的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方法針對(duì)http返回80,針對(duì)https返回443,其他的拋出UnsupportedSchemeException
小結(jié)
HttpClient的HttpRoutePlanner接口定義了determineRoute方法,用于決定該請(qǐng)求的目標(biāo)route;
DefaultRoutePlanner實(shí)現(xiàn)了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0時(shí)通過(guò)schemePortResolver.resolve來(lái)確定port(http返回80,https返回443)。
以上就是HttpClient HttpRoutePlanner接口確定請(qǐng)求目標(biāo)路由的詳細(xì)內(nèi)容,更多關(guān)于HttpClient HttpRoutePlanner接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- httpclient connect連接請(qǐng)求方法源碼解讀
- httpclient getPoolEntryBlocking連接池方法源碼解讀
- httpclient staleConnectionCheckEnabled獲取連接流程解析
- 解讀httpclient的validateAfterInactivity連接池狀態(tài)檢測(cè)
- httpclient的disableConnectionState方法工作流程
- 探索HttpClient中的close方法及其對(duì)連接的影響
- HttpClient的RedirectStrategy重定向處理核心機(jī)制
- httpclient ConnectionHolder連接池連接保持源碼解析
相關(guān)文章
Java JDBC連接Kerberos認(rèn)證的HIVE和Impala方式
本文主要介紹了HiveJDBC和ImpalaJDBC的使用方法,包括版本對(duì)應(yīng)、Maven安裝、主機(jī)名配置、端口開通、JDBC連接和Kerberos認(rèn)證等2025-02-02
Spring框架中TaskExecutor類型Bean沖突導(dǎo)致的自動(dòng)注入失敗問(wèn)題的解決步驟
Spring中多個(gè)TaskExecutor Bean沖突導(dǎo)致注入失敗,本文給大家介紹的解決方案包括使用@Qualifier或@Primary明確注入目標(biāo)、重命名Bean、排除自動(dòng)配置,確保版本兼容以避免沖突,需要的朋友可以參考下2025-07-07
Spring?boot框架JWT實(shí)現(xiàn)用戶賬戶密碼登錄驗(yàn)證流程
這篇文章主要介紹了Springboot框架JWT實(shí)現(xiàn)用戶賬戶密碼登錄驗(yàn)證,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Flyway詳解及Springboot集成Flyway的詳細(xì)教程
Flayway是一款數(shù)據(jù)庫(kù)版本控制管理工具,,支持?jǐn)?shù)據(jù)庫(kù)版本自動(dòng)升級(jí),Migrations可以寫成sql腳本,也可以寫在java代碼里。這篇文章主要介紹了Flyway詳解及Springboot集成Flyway的詳細(xì)教程的相關(guān)資料,需要的朋友可以參考下2020-07-07
Spring Data Redis 中的 opsFor 方法深入解析
opsFor是RedisTemplate類中的一系列方法,用于獲取特定數(shù)據(jù)類型的操作接口,這些方法返回的是 Operations 對(duì)象,每個(gè)對(duì)象都提供了針對(duì)特定 Redis 數(shù)據(jù)類型的操作方法,本文重點(diǎn)介紹Spring Data Redis 中的opsFor方法,感興趣的朋友一起看看吧2025-12-12

