我正在尝试通过带有Apache httpclient的代理发送HTTPS请求,但是在代理端找不到标头



我正试图通过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]用户代理:[Apache HttpClient/4.3.6(java 1.5)]

首先,这不是对代理进行身份验证的方式。其次,这些报头被添加到get请求(而不是proxy)。最后,这是基于一个示例HttpClient示例,特别是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();
}

最新更新