我正在尝试通过apache httpclient的代理发送https请求,但我找不到代理端的头文件

时间:2015-04-09 03:55:46

标签: java android https proxy httpclient

我试图通过代理使用apache httpclient发送https请求,但我无法在代理端找到标头

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);

代理端只查找代理连接和用户代理: 代理连接:[Keep-Alive] User-Agent:[Apache-HttpClient / 4.3.6(java 1.5)]

1 个答案:

答案 0 :(得分:1)

首先,这不是您对代理进行身份验证的方式。其次,这些标头会添加到get请求中(而不是proxy)。最后,这是基于HttpClient examples的示例 - 具体为ClientProxyAuthentication并更新为使用try-with-resources(并修改为使用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();
}