我通过代理服务器发送http请求。如何获取java http请求中的代理响应?
当我使用curl
我得到的回应两个部分组成:从代理和目标服务器:
curl -i --proxy proxy.com:8080 --proxy-user user:1234 https://target.com
HTTP/1.1 200 Connection established
X-Proxy-Header: Header-Value: connected established
HTTP/1.1 200 OK
Server: target.com
other headers
http response
我怎样才能在Java中相同的输出?我使用apache HttpClient,它只返回来自目标服务器的响应。代理响应丢失。
我需要的基本思路,而不是具体的实现,因为我也必须重用它的Netty库。总之,示例代码,开始从:
HttpHost myProxy = new HttpHost("proxy.com", 8080);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(myProxy), new UsernamePasswordCredentials("user", "1234"));
HttpClient client = HttpClients.custom()
.setConnectionManager(new BasicHttpClientConnectionManager())
.setProxy(myProxy)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
HttpGet request = new HttpGet(url);
HttpResponse = client.execute(response);
是的,这看起来太复杂了。其实我需要X-Proxy-Header的价值。记录对我来说是不够的,但我猜测日志记录对侦听器/拦截器起作用。我会尝试找出是否有可能附加到他们。 – AdamSkywalker