【发布时间】:2013-06-13 20:03:33
【问题描述】:
我使用 Apache Commons HttpClient 3.1 作为一种反向代理。该代理服务器在 8081 端口的 servlet 容器中运行,并将一些请求代理到同一台服务器上的 8080 端口。由于端口 8080 上的旧服务器使用 HTTP Host 标头构建了一些绝对 URL,因此我想显式设置该标头。
It is not possible to set the Host-header as you set other headers,因为 HttpClient 会自动覆盖您设置的值。我发现更改 Host-header 的唯一方法是设置虚拟主机:
HttpClient = ...
HttpMethod = ...
HostParams hostParams = new HostParams();
hostParams.setVirtualHost("localhost:8081");
hostConfiguration.setParams(hostParams);
hostConfiguration.setHost("localhost", 8080);
client.executeMethod(hostConfiguration, method);
但这并不能正常工作,因为 HttpClient 似乎将它连接的端口添加到Host:
11:07:05.011 [qtp1813719644-21] DEBUG httpclient.wire.header - >> "Host: localhost:8081:8080[\r][\n]"
有什么办法可以解决这个问题吗?如果不是,Apache Httpclient 4.x 的行为是否有所不同?
【问题讨论】:
-
我认为您不需要设置端口,主机可以更改,因为许多主机名可以具有相同的 IP,但只有端口:您要打开连接的那个.
hostParams.setVirtualHost("localhost"); -
@gma 这将使遗留应用程序发送“位置:localhost:8080∕foo”,并在 url 中使用端口 8080 的其他地方创建一些链接。 The port is a part of the Host-header.
-
是的,但除了 HttpClient 不相关的代理之外,它不相关。
-
@gma 好吧,它现在对我来说很重要:) HttpClient 提供了我所需要的,除了这个问题
-
@gma 使用代理(使用 8080 作为代理)。这使得所有请求都具有绝对 URL,但我没有发现任何实际问题。如果您添加答案:hostConfiguration.setProxy("localhost", 8080); hostConfiguration.setHost("localhost", 8081);我会接受的:)
标签: java apache-httpclient-4.x apache-commons-httpclient