The httpclient version of the article is 3.
There are two methods to implement SSL access.
The first method is to export the required certificate through a browser and then apply the certificate to the code.
The second method implements SSL management and directly receives the certificate from the server.
This article only discusses the second method.
This method implements a secureprotocolsocketfactory to process SSL.
Import Java. io. ioexception; import java.net. inetaddress; import java.net. inetsocketaddress; import java.net. socket; import java.net. socketaddress; import java.net. unknownhostexception; import Java. security. keymanagementexception; import Java. security. nosuchalgorithmexception; import Java. security. cert. certificateexception; import Java. security. cert. x509certificate; import javax.net. socketfactory; import javax.net. SSL. sslcontext; import javax.net. SSL. trustmanager; import javax.net. SSL. x509trustmanager; import Org. apache. commons. httpclient. connecttimeoutexception; import Org. apache. commons. httpclient. params. httpconnectionparams; import Org. apache. commons. httpclient. protocol. optional;/*** @ author yansheng723@gmail.com */public class implements secureprotocolsocketfactory {private sslcontext = NULL; private sslcontext createsslcontext () {sslcontext = NULL; try {sslcontext = sslcontext. getinstance ("SSL"); sslcontext. init (null, new trustmanager [] {New trustanytrustmanager ()}, new Java. security. securerandom ();} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (keymanagementexception e) {e. printstacktrace ();} return sslcontext;} private sslcontext getsslcontext () {If (this. sslcontext = NULL) {This. sslcontext = createsslcontext ();} return this. sslcontext;} public socket createsocket (Socket socket, string host, int port, Boolean autoclose) throws ioexception, unknownhostexception {return getsslcontext (). getsocketfactory (). createsocket (socket, host, port, autoclose);} public socket createsocket (string host, int port) throws ioexception, unknownhostexception {return getsslcontext (). getsocketfactory (). createsocket (host, Port);} public socket createsocket (string host, int port, inetaddress clienthost, int clientport) throws ioexception, unknownhostexception {return getsslcontext (). getsocketfactory (). createsocket (host, port, clienthost, clientport);} public socket createsocket (string host, int port, inetaddress localaddress, int localport, httpconnectionparams Params) throws ioexception, unknownhostexception, connecttimeoutexception {If (Params = NULL) {Throw new illegalargumentexception ("parameters may not be null");} int timeout = Params. getconnectiontimeout (); socketfactory = getsslcontext (). getsocketfactory (); If (timeout = 0) {return socketfactory. createsocket (host, port, localaddress, localport);} else {Socket socket = socketfactory. createsocket (); socketaddress localaddr = new inetsocketaddress (localaddress, localport); socketaddress remoteaddr = new inetsocketaddress (host, Port); socket. BIND (localaddr); socket. connect (remoteaddr, timeout); Return socket ;}// custom private class Private Static class trustanytrustmanager implements x509trustmanager {public void checkclienttrusted (x509certificate [] Chain, string authtype) throws certificateexception {} public void checkservertrusted (LOGIN [] Chain, string authtype) throws certificateexception {} public login [] getacceptedissuers () {return New x509certificate [] {}}}}
The factory that calls the implementation and registers it to protocol.
@SuppressWarnings("deprecation")Protocol protocol = new Protocol("https", newMySecureProtocolSocketFactory (), 443) {};Protocol.registerProtocol("https", protocol);