0

i'm trying to send a https request through a proxy with apache httpclient,but i can't find the headers on the proxy side

HttpClient httpClient =new DefaultHttpClient();
HttpHost proxy = new HttpHost("10.1.1.100", 8080);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,proxy);
HttpGet get = new HttpGet(uri);
get.addHeader("Proxy-Authorization", "222222");
HttpResponse hr = defaultHttpClient.execute(get);

the proxy side only find proxy-connection and user-agent: Proxy-Connection:[Keep-Alive] User-Agent:[Apache-HttpClient/4.3.6 (java 1.5)]

1 Answer 1

1

First, that's not how you authenticate to a proxy. Second, those headers are added to the get request (not to the proxy). Finally, this is based on an example the HttpClient examples - specifically ClientProxyAuthentication and updated to use try-with-resources (and modified to use an URL)

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("10.1.1.100", 8080),
        new UsernamePasswordCredentials("username", "password"));
try (CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultCredentialsProvider(credsProvider).build()) {
    URL url = new URL(uri);
    HttpHost target = new HttpHost(url.getHost(), url.getPort(),
            url.getProtocol());
    HttpHost proxy = new HttpHost("10.1.1.100", 8080);

    RequestConfig config = RequestConfig.custom().setProxy(proxy)
            .build();
    HttpGet httpget = new HttpGet(url.getPath());
    httpget.setConfig(config);
    System.out.println("Executing request " + httpget.getRequestLine()
            + " to " + target + " via " + proxy);

    try (CloseableHttpResponse response = httpclient.execute(target,
            httpget)) {

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        EntityUtils.consume(response.getEntity());
    }
} catch (IOException e1) {
    e1.printStackTrace();
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.