HTTPS requests do not ignore certificate methods
Load Original KeyStore mode
in the first chapter we generate a certificate and put it under the C://tomcat.keystore, we use this certificate to complete the request
Package Httpsclient;import Java.io.BufferedReader; Import Java.io.FileInputStream; Import java.io.IOException; Import Java.io.InputStreamReader; Import java.net.MalformedURLException; Import Java.net.URL; Import java.security.GeneralSecurityException; Import Java.security.KeyStore; Import Javax.net.ssl.HostnameVerifier; Import javax.net.ssl.HttpsURLConnection; Import Javax.net.ssl.KeyManagerFactory; Import Javax.net.ssl.SSLContext; Import Javax.net.ssl.TrustManagerFactory; public class Httpspost {/** * obtains keystore. * @param keystorepath * keystore path * @param password * password * @return KeyStore * @throw S Exception */public static KeyStore Getkeystore (string password, string keystorepath) throws Exce ption {//instantiate keystore KeyStore ks = keystore.getinstance ("JKS"); Get KeyStore file stream FileInputStream is = new FileInputStream (Keystorepath); Load KeyStore ks.load (IS, Password.tochararray ()); Close KeyStore file stream Is.close (); return KS; }/** * Get sslsocketfactory. * @param password * password * @param keystorepath * KeyStore path * @param truststorepath * Truststore Path * @return sslsocketfactory * @throws Exception */public static Sslcontext Getssl Context (string password, string keystorepath, String truststorepath) throws Exception {//instantiation KeyStore Keymanagerfactory keymanagerfactory = keymanagerfactory. getinstance (Keymanagerfactory.getdefaulta Lgorithm ()); Get keystore KeyStore KeyStore = getkeystore (password, keystorepath); Initialize key factory Keymanagerfactory.init (KeyStore, Password.tochararray ()); Instantiate truststore trustmanagerfactory trustmanagerfactory = trustmanagerfactory. getinstance (trustmanage Rfactory.getdefaultalgorithm ()); Get the Trust library KeyStore truststore = getkeystore (password, truststorepath); Initializes the Truststore trustmanagerfactory.init (Truststore); Instantiate the SSL context Sslcontext CTX = sslcontext.getinstance ("TLS"); Initializing the SSL Context Ctx.init (Keymanagerfactory.getkeymanagers (), Trustmanagerfactory.gettrustmanagers () , null); Get sslsocketfactory return CTX; }/** * initializes the httpsurlconnection. * @param password * password * @param keystorepath * KeyStore path * @param truststorepath * Truststore Path * @throws Exception */public static void Inithttpsurlconnection (String password, String Keystorepath, String truststorepath) throws Exception {//Declaration SSL Context Sslcontext Sslcontex t = null; Instantiate host name authentication interface Hostnameverifier HNV = new Myhostnameverifier (); try {sslcontext = getsslcontext (password, keystorepath, TruststorepATH); } catch (Generalsecurityexception e) {e.printstacktrace (); } if (Sslcontext! = null) {httpsurlconnection.setdefaultsslsocketfactory (sslcontext . Getsocketfactory ()); } httpsurlconnection.setdefaulthostnameverifier (HNV); }/** * Send request. * @param Httpsurl * Requested address * @param XMLSTR * Requested data */public static void Pos T (string Httpsurl, String xmlstr) {httpsurlconnection Urlcon = null; try {Urlcon = (httpsurlconnection) (New URL (Httpsurl)). OpenConnection (); Urlcon.setdoinput (TRUE); Urlcon.setdooutput (TRUE); Urlcon.setrequestmethod ("POST"); Urlcon.setrequestproperty ("Content-length", String.valueof (Xmlstr.getbytes (). Length)); Urlcon.setusecaches (FALSE); Set to GBK to resolve the data that is read by the server when it is received in Chinese garbled urlcon.Getoutputstream (). Write (Xmlstr.getbytes ("GBK")); Urlcon.getoutputstream (). Flush (); Urlcon.getoutputstream (). Close (); BufferedReader in = new BufferedReader (New InputStreamReader (Urlcon.getinputstream ())); String Line; while (line = In.readline ())! = null) {System.out.println (line); }} catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } catch (Exception e) {e.printstacktrace (); }}/** * Test method. * @param args * @throws Exception */public static void main (string[] args) throws Exception {// Password String password = "870515"; KeyStore String Keystorepath = "C:\\tomcat.keystore"; Trust library String truststorepath = "C:\\tomcat.keystore"; HTTPS service from the local String Httpsurl = "Https://localhost:8443/httpsDemo/hs"; Transfer text String xmlstr = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? ><fruitshop><fruits><fruit ><kind> Radish </kind></fruit><fruit><kind> Pineapple </kind></fruit></fruits ></fruitShop> "; Httpspost.inithttpsurlconnection (password, keystorepath, Truststorepath); Initiating the request Httpspost.post (Httpsurl, XMLSTR); } }
Package Httpsclient;import Javax.net.ssl.HostnameVerifier; Import javax.net.ssl.SSLSession; /** * Implements the base interface for host name validation. * During the handshake, if the host name of the URL does not match the identity host name of the server, the authentication mechanism can callback the implementation of this interface to determine whether this connection should be allowed. * /public class Myhostnameverifier implements Hostnameverifier { @Override public Boolean Verify ( String hostname, sslsession session) { if ("localhost". Equals (hostname)) { return true; } else { return false;}} }
Load Export CER method
HTTPS client sends request (III)