单元测试apache http客户端代理407

时间:2015-01-26 15:00:39

标签: apache junit4 apache-httpclient-4.x http-proxy

我正在尝试构建需要联系外部服务器的Junit / Integration测试。但是,我无法通过代理。我收到407空白身份验证页错误。

我使用的测试设置

@Before
public void onSetUp() throws Exception {

    System.setProperty("http.proxyHost", "webproxy-nl.test.com");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("https.proxyHost", "webproxy-nl.test.com");
    System.setProperty("https.proxyPort", "8080");

    System.setProperty("http.proxyUser", "DOM\\lalal");
    System.setProperty("http.proxyPassword", "tesssst");

    System.setProperty("https.proxyUser", "DOM\\lalala");
    System.setProperty("https.proxyPassword", "sldjsdkl");
}

现在所有代理设置都是100%正确的。我还添加了一些nonProxyhosts。

我不知道我还能配置什么。

返回消息是:

Http request failed: HTTP/1.1 407 BlankAuthenticationPage [status code 407]

更新

我构建了一个使用CloseableHttpClient的测试存根。这仍然给我http 407错误。

private CloseableHttpClient httpClient;

public IDealHttpClientStub() {

    LOG.debug("Creating IDealHttpClientStub");

    System.setProperty("http.proxyHost", "webproxy-nl.test.com");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("https.proxyHost", "webproxy-nl.test.com");
    System.setProperty("https.proxyPort", "8080");

    System.setProperty("http.proxyUser", "DOM\\lalal");
    System.setProperty("http.proxyPassword", "tesssst");

    System.setProperty("https.proxyUser", "DOM\\lalala");
    System.setProperty("https.proxyPassword", "sldjsdkl");

    this.httpClient = HttpClients.custom().useSystemProperties().build();
}

2 个答案:

答案 0 :(得分:0)

除非明确配置,否则HttpClient不会使用系统属性。

CloseableHttpClient client1 = HttpClients.createSystem();
CloseableHttpClient client2 = HttpClients.custom()
        .useSystemProperties()
        .build();

答案 1 :(得分:0)

我使用以下impl完成了它。

private CloseableHttpClient httpClient;

public HttpClientStub() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

    // Trust all certs
    SSLContext sslcontext = buildSSLContext();

    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    HttpHost proxy = new HttpHost("someproxy", 8080, "http");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    credsProvider.setCredentials(
            new AuthScope("someproxy", 8080, AuthScope.ANY_HOST, "ntlm"), new NTCredentials(
                    "itsme", "xxxx", "", "MYDOMAIN"));

    this.httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .setRoutePlanner(routePlanner).setSSLSocketFactory(sslsf).build();

}

private static SSLContext buildSSLContext() throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException {
    SSLContext sslcontext = SSLContexts.custom().setSecureRandom(new SecureRandom())
            .loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
                        throws java.security.cert.CertificateException {                        
                    return true;
                }
            }).build();
    return sslcontext;
}

然后您的代理配置了HTTP(测试)客户端。