The code is as follows
@Test public void testhtmlunit () throws exception { webclient client = new webclient ( Browserversion.chrome);// client.getoptions (). SetUseInsecureSSL (true); htmlpage page = client.getpage ("https:// 10.167.33.50/user "); htmlelement username = Page.getelementbyname ("Usrname"); username.type ("ns25000"); htmlelement pwd = page.getelementbyname ("Pass") ; pwd.type ("ns25000"); page.executejavascript ("Submitbutton" (' Login ', ' loginform '); system.out.print (PAGE.GETWEbresponse (). getcontentasstring ()); }
Run errors are as follows
javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexception: pkix path Building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable to find valid certification path to requested targetat Com.sun.net.ssl.internal.ssl.Alerts.getSSLException (alerts.java:174) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal (sslsocketimpl.java:1731) at Com.sun.net.ssl.internal.ssl.Handshaker.fatalSE (handshaker.java:241) at Com.sun.net.ssl.internal.ssl.Handshaker.fatalSE (handshaker.java:235) at Com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate (clienthandshaker.java:1206) at Com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage (clienthandshaker.java:136) at Com.sun.net.ssl.internal.ssl.Handshaker.processLoop (handshaker.java:593) at Com.sun.net.ssl.internal.ssl.Handshaker.process_record (handshaker.java:529) at com.sun.net.ssl.internal.Ssl. Sslsocketimpl.readrecord (sslsocketimpl.java:925) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake (sslsocketimpl.java:1170) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake (sslsocketimpl.java:1197) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake (sslsocketimpl.java:1181) at Org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket (sslconnectionsocketfactory.java:261) at Com.gargoylesoftware.htmlunit.HtmlUnitSSLConnectionSocketFactory.connectSocket ( htmlunitsslconnectionsocketfactory.java:155) at Org.apache.http.impl.conn.HttpClientConnectionOperator.connect (httpclientconnectionoperator.java:118) at Org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect (Poolinghttpclientconnectionmanager.java : 314) At org.apache.http.impl.execchain.mainclientexec.establishroute (mainclientexec.java:357) at Org.apache.http.impl.execchain.MainClientExec.execute (mainclientexec.java:218) At org.apache.Http.impl.execchain.ProtocolExec.execute (protocolexec.java:194) at Org.apache.http.impl.execchain.RetryExec.execute (retryexec.java:85) at Org.apache.http.impl.execchain.RedirectExec.execute (redirectexec.java:108) at Org.apache.http.impl.client.InternalHttpClient.doExecute (internalhttpclient.java:186) at Org.apache.http.impl.client.CloseableHttpClient.execute (closeablehttpclient.java:72) at Com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse (httpwebconnection.java:178) at Com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection (webclient.java:1313) at Com.gargoylesoftware.htmlunit.WebClient.loadWebResponse (webclient.java:1230) at Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:338) at Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:407) at Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:392) at Com.fnst.es.test.TestEs.testHtmlUnit (testes.java:72) At sun.reflect.nativemethODACCESSORIMPL.INVOKE0 (Native method) At sun.reflect.nativemethodaccessorimpl.invoke ( nativemethodaccessorimpl.java:39) At sun.reflect.delegatingmethodaccessorimpl.invoke ( DELEGATINGMETHODACCESSORIMPL.JAVA:25) At org.junit.runners.model.frameworkmethod$1.runreflectivecall ( frameworkmethod.java:47) At org.junit.internal.runners.model.reflectivecallable.run ( REFLECTIVECALLABLE.JAVA:12) at org.junit.runners.model.frameworkmethod.invokeexplosively ( frameworkmethod.java:44) At org.junit.internal.runners.statements.invokemethod.evaluate (InvokeMethod.java : At org.junit.runners.parentrunner.runleaf (parentrunner.java:271) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:70) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:50) at Org.junit.runners.parentrunner$3.run (parentrunner.java:238) At org.junit.runners.parentrunner$1.schedule ( PARENTRUNNER.JAVA:63) at org.junit.runners.parentrUnner.runchildren (parentrunner.java:236) at org.junit.runners.parentrunner.access$000 (ParentRunner.java:53 ) at org.junit.runners.parentrunner$2.evaluate (parentrunner.java:229) at Org.junit.runners.ParentRunner.run (parentrunner.java:309) At org.junit.runner.junitcore.run (Junitcore.java : At com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs (junit4ideatestrunner.java:74) at com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart (junitstarter.java:211) at Com.intellij.rt.execution.junit.JUnitStarter.main (junitstarter.java:67) at SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (Native method) at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:39) at Com.intellij.rt.execution.application.AppMain.main (appmain.java:134) caused by: sun.security.validator.validatorexception: pkix path building failed: Sun.security.provider.certpath.suncertpathbuilderexception: unable to find valid certification path to requested targetat Sun.security.validator.PKIXValidator.doBuild (pkixvalidator.java:323) at Sun.security.validator.PKIXValidator.engineValidate (pkixvalidator.java:217) at Sun.security.validator.Validator.validate (validator.java:218) at Com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate (x509trustmanagerimpl.java:126) at Com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted (x509trustmanagerimpl.java:209) at Com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted (x509trustmanagerimpl.java:249) at Com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate (clienthandshaker.java:1185) ... 51 morecaused by: sun.security.provider.certpath.suncertpathbuilderexception: unable to find valid certification path to requested targetat Sun.security.provider.certpath.SunCertPathBuilder.engiNebuild (suncertpathbuilder.java:174) at java.security.cert.certpathbuilder.build (CertPathBuilder.java:238) At sun.security.validator.pkixvalidator.dobuild (pkixvalidator.java:318) ... 57 more
The certificate that should be HTTPS has expired or is not trusted. Google, found that Htmlunit is also using httpclient, so the use of httpclient solutions
Sslcontext sslcontext = sslcontext.getinstance ("SSL");// set up a Trustmanager that trusts everythingsslcontext.init (null, new trustmanager[] { new x509trustmanager () { public x509certificate[] getacceptedissuers () { system.out.println (" getacceptedissuers ============= "); return null; } Public void checkclienttrusted (x509certificate[] certs, &nBsp; string authtype) { system.out.println ("checkclienttrusted ============="); } Public void checkservertrusted (x509certificate[] certs, string authtype) { system.out.println ("checkServerTrusted ============= "); }} }, new securerandom ()); SslsocKetfactory sf = new sslsocketfactory (Sslcontext); Scheme httpsscheme = new scheme ("https",  443, SF); Schemeregistry schemeregistry = new schemeregistry (); Schemeregistry.register (HttpsScheme) ;// apache httpclient version >4.2 should use Basicclientconnectionmanagerclientconnectionmanager cm = new singleclientconnmanager ( Schemeregistry); Httpclient httpclient = new defaulthttpclient (CM);
The answer to the above is fruitless ...
Continue Google's (big Google can not use, there are Valley search customers)
Find the following
How to ignore SSL certificate error
Solution:
For future Reference if someone wants to does the same thing its fairly
straight forward.
1) Create a package called
Org.apache.commons.httpclient.contrib.ssl
add the files.
Easysslprotocolsocketfactory.java
Easyx509trustmanager.java
Compile.
2) Add the following lines of code to your htmlclient before making any
Htmlunit calls
Protocol Easyhttps = new Protocol ("https", new
easysslprotocolsocketfactory (), 443);
protocol.registerprotocol ("https", Easyhttps);
The corresponding two Java files are as follows
http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/ Ssl/easysslprotocolsocketfactory.java?view=markup
http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/ Ssl/easysslprotocolsocketfactory.java?view=markup
It still doesn't work after you modify it.
Finally see the author description
FYI, starting with version 1.14 You can use WEBCLIENT.SETUSEINSECURESSL (true) instead, and it'll take care of all the Ht Tpclient configuration behind the scenes.
So excited ...
Use is to find WebClient without this method, which has been extracted from the Webclientoptions class.
/** * If set to <CODE>TRUE</CODE> The client would accept connections to any host, regardless of * whether the Y has valid certificates or not. This was especially useful when you were trying to * connect to a server with expired or corrupt certificates. * @param useinsecuressl Whether or not to use insecure SSL */public void Setuseinsecuressl (Final Boolean Useinsecuressl) { Useinsecuressl_ = Useinsecuressl;}
So modify the code to
@Testpublic void Testhtmlunit () throws Exception {WebClient client = new WebClient (browserversion.chrome); Client.getoptions (). Setuseinsecuressl (True); HtmlPage page = client.getpage ("Https://10.167.33.50/user"); HtmlElement username = page.getelementbyname ("Usrname"); Username.type ("ns25000"); HtmlElement pwd = page.getelementbyname ("Pass"); Pwd.type ("ns25000"); Page.executejavascript ("Submitbutton" (' Login ', ' loginform '); System.out.print (Page.getwebresponse (). getcontentasstring ());}
ok~
Htmlunit resolving HTTPS Certificate distrust issues