根据this FAQ entry,似乎HttpUnit正在使用Java标准库提供的SSL实现。编写和安装“照单全收” TrustManager
很简单:
private static class AnyTrustManager implements X509TrustManager
{
public void checkClientTrusted(X509Certificate[] chain, String authType)
{
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
{
}
public X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[0];
}
}
static {
try {
SSLContext ssl = SSLContext.getInstance("SSL");
ssl.init(null, new X509TrustManager[] {new AnyTrustManager()}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
} catch (NoSuchAlgorithmException ex) {
throw new Error(ex);
} catch (KeyManagementException ex) {
throw new Error(ex);
}
}
但是你应该记住,此代码示例可能需要一些修改与HttpUnit的工作(例如,如果使用自定义库建立连接的SocketFactory )
因为它似乎HttpUnit的不提供任何API来这里设置自定义SSLSocketFactry是一个替代的解决方案设置默认的SSL上下文(Java 6中只)
static {
try {
SSLContext ssl = SSLContext.getDefault();
ssl.init(null, new X509TrustManager[] {new AnyTrustManager()}, null);
} catch (NoSuchAlgorithmException ex) {
throw new Error(ex);
} catch (KeyManagementException ex) {
throw new Error(ex);
}
}
来源
2011-02-01 22:54:24
Jcs
谢谢,所以将标准WebConversation.getResponse()调用实现这个的TrustManager? – TheWolf 2011-02-01 23:02:18