单元测试apache http客户端代理407
unit test apache http client proxy 407
我正在尝试构建一个需要联系外部服务器的 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% 正确的。我也添加了一些非代理主机。
我不知道我还能在这里配置什么。
return 消息是:
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();
}
HttpClient 不使用系统属性,除非明确配置为这样做。
CloseableHttpClient client1 = HttpClients.createSystem();
CloseableHttpClient client2 = HttpClients.custom()
.useSystemProperties()
.build();
我已经使用以下实现完成了它。
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(测试)客户端。
我正在尝试构建一个需要联系外部服务器的 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% 正确的。我也添加了一些非代理主机。
我不知道我还能在这里配置什么。
return 消息是:
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();
}
HttpClient 不使用系统属性,除非明确配置为这样做。
CloseableHttpClient client1 = HttpClients.createSystem();
CloseableHttpClient client2 = HttpClients.custom()
.useSystemProperties()
.build();
我已经使用以下实现完成了它。
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(测试)客户端。