【问题标题】: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我正在尝试使用 apache httpclient 通过代理发送 https 请求,但在代理端找不到标头
【发布时间】:2015-06-14 06:50:13
【问题描述】:

我正在尝试使用 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)]

【问题讨论】:

    标签: java android https proxy httpclient


    【解决方案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();
    }
    

    【讨论】:

    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2017-04-28
    • 2015-10-16
    • 1970-01-01
    • 2018-07-16
    相关资源
    最近更新 更多