One, because the use of HTTPS when sending requests will be involved in the authentication method. But this is not a convenient way to use it. Especially when requesting an external interface, so I wrote a way to skip validation. (for reference)
Second, add the package, here is the package of Commons-httpclient 3.1. General requests with the latest httpclient4.5 can be
<dependency> <groupId>commons-httpclient</groupId> <artifactId> Commons-httpclient</artifactid> <version>3.1</version></dependency>
Three, here we implement 3 classes
1, Myx509trustmanager (this method directly implemented X509trustmanager,x509trustmanager in Javax.net.ssl.X509TrustManager)
There's no need to change anything directly here.
Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;ImportJavax.net.ssl.X509TrustManager; Public classMyx509trustmanagerImplementsX509trustmanager {/*(non-javadoc) * @see javax.net.ssl.x509trustmanager#checkclienttrusted (java.security.cert.x509certificate[], java.lang.String)*/ Public voidcheckclienttrusted (x509certificate[] arg0, String arg1)throwscertificateexception {}/*(non-javadoc) * @see javax.net.ssl.x509trustmanager#checkservertrusted (java.security.cert.x509certificate[], java.lang.String)*/ Public voidcheckservertrusted (x509certificate[] arg0, String arg1)throwscertificateexception {}/*(non-javadoc) * @see javax.net.ssl.x509trustmanager#getacceptedissuers ()*/ Publicx509certificate[] Getacceptedissuers () {return NULL; }}
2, mysecureprotocolsocketfactory (here we need to use the Sslcontext, but also need to rewrite a method to implement Secureprotocolsocketfactory)
Importjava.io.IOException;Importjava.net.InetAddress;ImportJava.net.Socket;Importjava.net.UnknownHostException;ImportJavax.net.ssl.SSLContext;ImportJavax.net.ssl.TrustManager;Importorg.apache.commons.httpclient.ConnectTimeoutException;ImportOrg.apache.commons.httpclient.HttpClientError;ImportOrg.apache.commons.httpclient.params.HttpConnectionParams;Importorg.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;Importorg.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; Public classMysecureprotocolsocketfactoryImplementsSecureprotocolsocketfactory {//Add a property here, the main purpose is to get SSL skip verification PrivateSslcontext Sslcontext =NULL; /*** Constructor for Mysecureprotocolsocketfactory. */ Publicmysecureprotocolsocketfactory () {}/*** This creates a method to get Sslcontext, import Myx509trustmanager to initialize *@return */ Private StaticSslcontext Createeasysslcontext () {Try{Sslcontext Context= Sslcontext.getinstance ("SSL"); Context.init (NULL,NewTrustmanager[] {NewMyx509trustmanager ()},NULL); returncontext; } Catch(Exception e) {Throw NewHttpclienterror (e.tostring ()); } } /*** Judge Get Sslcontext *@return */ PrivateSslcontext Getsslcontext () {if( This. Sslcontext = =NULL) { This. Sslcontext =Createeasysslcontext (); } return This. Sslcontext; } //the next method is basically to bring in the relevant parameters. /** (Non-javadoc) * * @see Org.apache.commons.httpclient.protocol.protocolsocketfactory#createsocket (JAVA.L Ang. String, * int, java.net.InetAddress, int)*/ PublicSocket Createsocket (String host,intPort, inetaddress clienthost,intClientPort)throwsIOException, unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (Host, Port,clienthost, ClientPort); } /** (Non-javadoc) * * @see Org.apache.commons.httpclient.protocol.protocolsocketfactory#createsocket (JAVA.L Ang. String, * int, java.net.InetAddress, int, * org.apache.commons.httpclient.params.HttpConnectionParams) */ PublicSocket Createsocket (FinalString Host,Final intPort,FinalInetAddress LocalAddress,Final intLocalPort,FinalHttpconnectionparams params)throwsioexception,unknownhostexception, connecttimeoutexception {if(Params = =NULL) { Throw NewIllegalArgumentException ("Parameters May is not NULL"); } intTimeout =params.getconnectiontimeout (); if(Timeout = 0) { returnCreatesocket (host, Port, localaddress, LocalPort); } Else { returnControllerthreadsocketfactory.createsocket ( This, Host, port,localaddress, LocalPort, timeout); } } /** (Non-javadoc) * * @see Secureprotocolsocketfactory#createsocket (java.lang.string,int)*/ PublicSocket Createsocket (String host,intPortthrowsioexception,unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (host, Port); } /** (Non-javadoc) * * @see Secureprotocolsocketfactory#createsocket (Java.net.socket,java.lang.string,int,bo Olean)*/ PublicSocket Createsocket (socket socket, String host,intPort,BooleanAutoClose)throwsIOException, unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (socket, host,port, autoClose); }}
3, then is httpclient, here the way to achieve is a single room, as long as the declaration mysecureprotocolsocketfactory join on it can be protocol
ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.protocol.Protocol;Importorg.apache.commons.httpclient.protocol.ProtocolSocketFactory;/** Tool class for Post requests with HttpClient*/ Public classHttpclientutil { Public StaticString doget (string url)throwsException {//StatementProtocolsocketfactory fcty =Newmysecureprotocolsocketfactory (); //join the relevant HTTPS request modeProtocol.registerprotocol ("https",NewProtocol ("https", Fcty, 443)); //send a requestOrg.apache.commons.httpclient.HttpClient httpclient =Neworg.apache.commons.httpclient.HttpClient (); GetMethod HttpGet=Newgetmethod (URL); System.out.println ("======url:" +URL); Try{Httpclient.executemethod (httpget); returnhttpget.getresponsebodyasstring (); } Catch(Exception ex) {ex.printstacktrace (); Throw NewException (Ex.getmessage ()); } finally{httpget.releaseconnection (); } }}
Four, here is basically completed, in the use of the time as long as the declaration mysecureprotocolsocketfactory join can be protocol, and then you can achieve the validation of the skipped
HttpClient Skip authentication for HTTPS requests