HttpClient如何执行处理过度指定的路径?

时间:2014-09-18 16:53:39

标签: java apache-httpclient-4.x apache-httpcomponents

我注意到org.apache.http.client.HttpClient指定了一个采用目标主机和请求参数的execute方法。

/**
 * Executes a request to the target using the default context.
 *
 * @param target    the target host for the request.
 *                  Implementations may accept <code>null</code>
 *                  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)
    throws IOException, ClientProtocolException;

我已经深入研究了DefaultHttpClientDefaultRequestDirector源代码,很明显,更简单的execute(HttpRequest request)方法将请求分离为HttpHost对象和HttpRequest对象并转发调用更具指定的方法(实际上,也是一个也接受HttpContext对象的方法 - 但我现在不关心上下文。)

我的问题是,既然请求和主机都可以接受URI,那么最终的URI是如何确定的?它们都包含绝对URI是什么?怎么会发生冲突?什么是他们都有部分路径?

1 个答案:

答案 0 :(得分:1)

HttpHost代表物理端点。它只包含URI的权限部分(方案,主机和端口)。它不包含路径。请求URI表示可能虚拟的资源。没有冲突。仅当未明确指定HttpHost时,才会将请求URI的权限部分视为物理端点

例如

HttpHost = http://www.google.com:-1
Request URI = http://www.google.ch/stuff 

将导致以下消息撰写

TCP

localhost:<random> -> www.google.com:80

HTTP

GET /stuff HTTP/1.1
Host: www.google.ch

你可以在AbstractHttpClient.determineTarget私有帮助器方法中看到这种行为,尽管从4.0到4.3.5之间发生了根本的变化,但是从execute的变体的URI中只提取了方案,主机和端口。不提供HttpHost。将目标和请求组成回最终路径的代码更复杂,但仔细阅读HttpRoute,HttpRoutePlanner和HttpRequest impl方法都清楚地引用了hosthostTarget来自HTTP主机和来自本地部分的路径(以及params和fragment)来自HttpRequest。