凯哥Java 凯哥java
前言:html
在使用htmlunit 爬取其余网站信息的时候,提示错误信息:unable to find valid certification path to requested targetjava
意思:web
说明证书问题。各类检索,使用了不少方法,如下记录解决思路:ide
解决方案一:网站
一种解决方案是: webClient.getOptions().setUseInsecureSSL(true);code
这么设置以后,确实没有在提示unable to find valid certification path to requested target错误了。可是引起了其余的问题。因此该方案不成立。htm
继续排查:blog
解决方案二:ip
设置setUseInsecureSSL(false);ssl
重要代码:
WebClient webClient = new WebClient(BrowserVersion.CHROME); /*** htmlunit unable to find valid certification path to requested target **/ webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常, 这里选择不须要 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不须要 webClient.getOptions().setActiveXNative(false); // webClient.getOptions().setCssEnabled(false);//是否启用CSS, 由于不须要展示页面, 因此不须要启用 webClient.getOptions().setJavaScriptEnabled(true); //很重要,启用JS webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,设置支持AJAX
添加上面代码问题完美解决。