代码之家  ›  专栏  ›  技术社区  ›  Ori Marko

httpclient-执行httprequest时何时使用httphost参数

  •  0
  • Ori Marko  · 技术社区  · 6 年前

    使用以下代码时:

    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的主机不同?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mohamed Anees A    6 年前

    从文档中,

    如果实现仍然可以确定路由,则它们可能接受空值。 例如默认目标或 通过检查请求 .

    你的问题

    httphost的主机可以由包含 统一资源定位地址

    只需解释文档中所写的内容。

    在你的情况下,一个电话接另一个电话是没有好处的。但是,对于高级配置,您可以参考 this 其中,您可能需要显式地使用httphost。希望这有帮助!