作为前锋,这是我的第一个 Java 程序,因此对我可能犯下的任何格式化暴行表示歉意。我的单元测试程序碰壁了。我无法使用默认界面,因此我将请求设置为在不同的本地地址上发出。但是,当我这样做时,SSL 不再起作用。这是我执行程序时收到的回溯。我正在寻找某种方法来在非默认接口(在我的情况下为 eth1)上发出请求并让 SSL 工作。
Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:371)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:172)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1460)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1379)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:311)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:380)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:365)
at UnitTest.main(UnitTest.java:65)
--
public class UnitTest {
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
webClient.setWebConnection(new HttpWebConnection(webClient) {
@Override
protected AbstractHttpClient createHttpClient() {
AbstractHttpClient client = super.createHttpClient();
try {
HttpParams params = client.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName("XXX.XXX.XXX.XXX"));
client.setParams(params);
}
catch(Exception e) {
e.printStackTrace();
}
return client;
}
});
webClient.setThrowExceptionOnScriptError(false);
webClient.setJavaScriptEnabled(false);
webClient.getCookieManager().setCookiesEnabled(true);
HtmlPage page = (HtmlPage)
webClient.getPage("https://someencryptedpage.com");