HttpClient - 执行 HttpRequest 时何时使用 HttpHost 参数

HttpClient - When to use HttpHost parameter when executing HttpRequest

使用以下代码时:

URIBuilder uriBuilder = new URIBuilder(url);
HttpPost httpRequest= new HttpPost(uriBuilder.build());
HttpHost httpHost= new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());

什么时候使用

是个好习惯
httpClient.execute(httpHost, httpRequest);

因为httpHost的host可以通过包含URL

的HttpRequest来确定
/**
 * Executes HTTP request using the default context.
 *
 * @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
 *
 * @return  the response to the request. This is always a final response,
 *          never an intermediate response with an 1xx status code.
 *          Whether redirects or authentication challenges will be returned
 *          or handled automatically depends on the implementation and
 *          configuration of this client.
 * @throws IOException in case of a problem or the connection was aborted
 * @throws ClientProtocolException in case of an http protocol error
 */
HttpResponse execute(HttpHost target, HttpRequest request)

比打电话有什么好处吗

httpClient.execute(httpRequest);

是proxy/firewall/balancer的相关解决方案吗?主机何时会不同于 URL 的主机?

来自文档,

Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.

你的问题

httpHost's host can be determined by HttpRequest which includes the URL

只是解释文档中的内容。

在你的情况下,调用一个而不调用另一个没有任何好处。但是,对于高级配置,您可以在明确需要使用 HTTPHost 的地方参考 this。希望这对您有所帮助!