3

作为前锋,这是我的第一个 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");
4

1 回答 1

4

这是由不信任此证书的 java 密钥库引起的。

最快的解决方案是将 Webclient 设置为忽略 SSL 异常:

对于 HtmlUnit pre 2.11 版本:

webClient.setUseInsecureSSL(true); 

对于 HtmlUnit 2.11 和更新版本:

webClient.getOptions().setUseInsecureSSL(true);

这是HtmlUnit javadoc

如果由于某些原因您想要管理证书以使其受信任而不是忽略它的状态,您可以将其导入您的密钥库,但这可能需要维护,例如如果证书已更改/更新:将证书添加到密钥库。这里有一些指示

于 2012-11-24T10:30:10.853 回答