根據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