我试图通过发送与Apache的HttpClient的代理HTTPS请求,但我不能找到代理方头 [英] 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

查看:503
本文介绍了我试图通过发送与Apache的HttpClient的代理HTTPS请求,但我不能找到代理方头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过发送与Apache的HttpClient的代理HTTPS请求,但我不能找到代理方头

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

代理方只能找代理连接和用户代理:
- 代理连接:保持活动]的User-Agent:Apache的HttpClient的/ 4.3.6(Java 1.5中)]

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

推荐答案

首先,这不是你如何进行身份验证的代理。其次,这些头被添加到 GET 要求(而不是代理)。最后,这是基于一个例子 HttpClient的范例 - 特别是<一个href=\"https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java\"相对=nofollow> ClientProxyAuthentication ,并更新为使用的 尝试-与资源 (和修改,以使用网​​址

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

这篇关于我试图通过发送与Apache的HttpClient的代理HTTPS请求,但我不能找到代理方头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆