| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 
 |  
HttpHost proxy = new HttpHost(proxyHost, 808);
    HttpHost target = new HttpHost(targetHost, 80);
    HttpGet req = new HttpGet("/default.asp");
    Credentials targetCredentials = new UsernamePasswordCredentials(targetUser, targetpassword);
    Credentials proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
 
    DefaultHttpClient httpclient = null;
    try {
        final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        httpclient = new DefaultHttpClient(connectionManager);
        HttpContext localContext = new BasicHttpContext();
        req.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        System.out.println("executing request to " + target + " via " + proxy);
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxy), proxyCredentials);
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(target), targetCredentials);
        HttpResponse rsp = httpclient.execute(target, req,localContext);
        AuthState proxyAuthState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
        System.out.println("Proxy auth state: " + proxyAuthState.getState());
        System.out.println("Proxy auth scheme: " + proxyAuthState.getAuthScheme());
        System.out.println("Proxy auth credentials: " + proxyAuthState.getCredentials());
        AuthState targetAuthState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
        System.out.println("Target auth state: " + targetAuthState.getState());
        System.out.println("Target auth scheme: " + targetAuthState.getAuthScheme());
        System.out.println("Target auth credentials: " + targetAuthState.getCredentials());
 
        HttpEntity entity = rsp.getEntity();
 
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }
 
    }catch (Exception e) {
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
 
} | 
Partager