0

使用以下代码时:

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 的主机可以通过包含 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);

它是代理/防火墙/平衡器相关的解决方案吗?主机何时与 URL 的主机不同?

4

1 回答 1

1

从文档中,

如果实现仍然可以确定路由,例如到默认目标或通过检查请求,则可以接受 null 。

你的问题

httpHost 的主机可以由包含 URL 的 HttpRequest 确定

只是解释文档中写的内容。

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

于 2019-01-02T06:00:09.260 回答