获得异常“SSLPeerUnverifiedException:peer not authenticated”,我该如何避免/忽略它?

时间:2012-04-13 14:51:28

标签: java automation screen-scraping htmlunit

我正在尝试使用HtmlUnit(Java Library)登录网站,并且我得到一个我觉得我可以忽略的异常。例外情况是“javax.net.ssl.SSLPeerUnverifiedException:peer not authenticated”。我已将其缩小到证书支持“www.thesite.com”,但它确实使用“somedomain.thesite.com”。我认为错误是由于证书没有命名它在某些javascript函数中使用的其他子域。这很奇怪,因为手动使用Firefox时,我没有看到这些异常,让我觉得还有其他事情发生。

以下是我正在使用的代码:

// <editor-fold desc="User Credentials and site.">
String user = "auser";
String password = "apassword";

String mainUrl = "https://www.awebsite.com/";

// Setup Ajax & Cookies
AjaxController ajaxController = new AjaxController();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiesEnabled(true);

// Setup WebClient
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setAppletEnabled(true);
webClient.setCssEnabled(true);
webClient.setJavaScriptEnabled(true);
webClient.setAjaxController(ajaxController);
webClient.setThrowExceptionOnScriptError(false);
webClient.setThrowExceptionOnFailingStatusCode(false);
webClient.setCookieManager(cookieManager);

/*
 * Provider p = new sun.security.pkcs11.SunPKCS11();
 * Security.addProvider(p);
 */

//webClient.set
HtmlPage page = null;

try {
    webClient.setUseInsecureSSL(true);
    page = webClient.getPage(mainUrl);

    // fill in form
    HtmlForm htmlForm = page.getFormByName("thisForm");
    HtmlInput userField = htmlForm.getInputByName("Username");
    HtmlInput passwordField = htmlForm.getInputByName("Password");

    // Enter login and password
    userField.setValueAttribute(user);
    passwordField.setValueAttribute(password);

    //HtmlPage mainMenuPage = htmlForm.click();
    HtmlElement element = htmlForm.getInputByValue("Login");

    page = element.click();
    webClient.waitForBackgroundJavaScript(5000); // pause if

    System.err.println(page.asText());
    System.err.println(page.asXml());

} catch (Exception ex) {
    ex.printStackTrace();
}

webClient.closeAllWindows();

}

如您所见,我启用了'webClient.setUseInsecureSSL(true);'在HtmlUnit库中。我想这应该这样做。但是,此时我被卡住了。

以下是我遇到的异常错误:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:152)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1358)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:307)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:358)
    at com.mycompany.extract.Program.main(Program.java:62)

任何想法或建议?该错误之后程序根本不会继续,我不在乎证书是否错误(它是一个有效的证书,只是使用不同的子域),证书是我无法控制的。

1 个答案:

答案 0 :(得分:0)

问题是您需要该子域的有效证书。获取它,并将其添加到您正在使用的Java版本的jssecacerts中。

我是使用InstallCert.java完成的。这里有一些有用的文档:http://miteff.com/install-cert,可能在这里:http://pinchii.com/home/tag/jssecacerts/

简单的谷歌搜索“InstallCert.java”应该会给你带来很多结果。

祝你好运!