Access an HTTPS website through httpclient

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.