Connect the Java client to the HTTPS Server

Source: Internet
Author: User
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java.net. url;

Import javax.net. SSL. hostnameverifier;
Import javax.net. SSL. httpsurlconnection;
Import javax.net. SSL. sslsession;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import java. Io. dataoutputstream;
Import java.net. httpurlconnection;
Import org. Apache. xerces. impl. DV. util. base64;

 

 

Public class ssltest {

   Protected string connect (string strurl ){

           TrustallhttpscertificateS ();

           Hostnameverifier HV = new hostnameverifier (){
                      Public Boolean verify (string urlhostname, sslsession session ){
                              System. Out. println ("Warning: url host:" + urlhostname + "."
                                     + Session. getpeerhost ());
                               Return true;
                        }
          };

           Httpsurlconnection. setdefaulthostnameverifiEr (HV );

          
           URL url = new URL (strurl );
           Httpurlconnection urlconn = (httpurlconnection) URL. openconnection ();
 Connect to the HTTPS Server
         ......

   }

    
   Private Static void trustallhttpscertificateS () throws exception {

       //Create a trust manager that does not validate certificate chains:

       Javax.net. SSL. trustmanager [] trustallcerts =
               New javax.net. SSL. trustmanager [1];

       Javax.net. SSL. trustmanager TM = new mitm ();

       Trustallcerts [0] = TM;

       Javax.net. SSL. sslcontext SC =
               Javax.net. SSL. sslcontext. getinstance ("SSL ");

       SC. INIT (null, trustallcerts, null );

       Javax.net. SSL. httpsurlconnection. setdefasssslsocketfactoRy (
               SC. getsocketfactory ());

   }

   Public static class mitm implements javax.net. SSL. trustmanager,
           Javax.net. SSL. x509trustmanager {
       Public java. Security. cert. x509certificate [] getacceptedissuers (){
           Return NULL;
       }

       Public Boolean isservertrusted (
               Java. Security. cert. x509certificate [] CERTS ){
           Return true;
       }

       Public Boolean isclienttrusted (
               Java. Security. cert. x509certificate [] CERTS ){
           Return true;
       }

       Public void checkservertrusted (
               Java. Security. cert. x509certificate [] certs, string authtype) throws
               Java. Security. cert. certificateexception {
           Return;
       }

       Public void checkclienttrusted (
               Java. Security. cert. x509certificate [] certs, string authtype) throws
               Java. Security. cert. certificateexception {
           Return;
       }
   }
   
}

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.