Apache HttpClient 4.3.1 中使用 HTTP 隧道/HTTPS 连接的抢先式代理身份验证

Posted

技术标签:

【中文标题】Apache HttpClient 4.3.1 中使用 HTTP 隧道/HTTPS 连接的抢先式代理身份验证【英文标题】:Preemptive Proxy Authentication with HTTP Tunnel / HTTPS Connection in Apache HttpClient 4.3.1 【发布时间】:2014-02-02 23:02:05 【问题描述】:

我正在尝试通过需要使用 Apache HttpClient 4.3.1 进行抢先身份验证的代理发送 HTTPS 请求。

当我在第一个请求中没有直接对自己进行身份验证时,我的代理会在几分钟内阻止来自我的 IP 的连接。

我对正常的 HTTP 请求没有任何问题,我只是在请求中手动添加了“Proxy-Authorization”标头。

但是当尝试加载 HTTPS 页面时,HttpClient 似乎使用 HTTP 隧道,因此第一个请求是“CONNECT”命令,然后发送我的实际请求。使用 request.setHeader(...) 方法不会影响 CONNECT 请求的标头,从而导致“HTTP/1.0 407 Proxy Authentication Required”响应和我的连接关闭。 之后,HttpClient 再次连接,这次使用我的凭据添加“Proxy-Authorization”标头字段。

连接成功(HTTP/1.0 200 连接已建立)并且我的实际 GET 请求正在执行。 但是当我在那之后再次运行我的程序时,我会得到一个 IOException:

信息:I/O 异常 (java.net.SocketException) 何时被捕获 处理请求:连接重置

在 Wireshark 中,我可以看到,代理不再响应我的“CONNECT”请求(不包含凭据)。 所以我尝试了几种方法让 HttpClient 在第一个 CONNECT 请求中发送凭据: 我调整了这个example 以使用代理并为代理创建了AuthCache,但它不起作用。 我还尝试向我的客户端添加一个 HttpRequestInterceptor:

static class PreemptiveAuth implements HttpRequestInterceptor 
    @Override
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException 
        request.setHeader("Proxy-Authorization", "Basic <base64credentials>");
    

但这也不影响“CONNECT”请求。这是我的其余代码:

public class ClientProxyAuthentication 

public static void main(String[] args) throws IOException, InterruptedException 
    HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
    HttpHost proxy = new HttpHost("<proxy-ip>", 21265, "http");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope("<proxy-ip>", 21265),
            new UsernamePasswordCredentials("username", "pass"));

    CloseableHttpClient httpclient = HttpClients.custom()
            .addInterceptorFirst(new PreemptiveAuth())
            .setProxy(proxy)
            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
            .setDefaultCredentialsProvider(credsProvider).build();


    try 

        HttpGet httpget = new HttpGet("/");
        httpget.setHeader("Proxy-Authorization", "Basic <base64credentials>");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("via proxy: " + proxy);
        System.out.println("to target: " + targetHost);

        CloseableHttpResponse response = httpclient.execute(targetHost, httpget);
        try 
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) 
                System.out.println("Response content length: " + entity.getContentLength());
            
            String html = EntityUtils.toString(entity, "UTF-8");
            System.out.println(html);
            EntityUtils.consume(entity);
         finally 
            response.close();
        
     finally 
        httpclient.close();
    

【问题讨论】:

【参考方案1】:

我怀疑您没有正确初始化身份验证缓存。请试试这个。

HttpHost proxy = new HttpHost("proxy", 8080);

BasicScheme proxyAuth = new BasicScheme();
// Make client believe the challenge came form a proxy
proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=default"));
BasicAuthCache authCache = new BasicAuthCache();
authCache.put(proxy, proxyAuth);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(proxy),
        new UsernamePasswordCredentials("username", "password"));

HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context.setCredentialsProvider(credsProvider);

CloseableHttpClient httpclient = HttpClients.createDefault();
try 
    CloseableHttpResponse response = httpclient.execute(new HttpGet("/stuff"), context);
    try 
        // ...
     finally 
        response.close();
    
 finally 
    httpclient.close();

【讨论】:

以上是关于Apache HttpClient 4.3.1 中使用 HTTP 隧道/HTTPS 连接的抢先式代理身份验证的主要内容,如果未能解决你的问题,请参考以下文章

新旧apache HttpClient 获取httpClient方法

在 Android SDK 23 中使用 Apache HttpClient。NoSuchMethod

Apache HttpClient Android (Gradle)

org.apache.http.client.HttpClient使用方法

Android 6.0 使用 Apache HttpClient

java模拟http请求上传文件,基于Apache的httpclient