我通過代理服務器發送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