Source code for requesting HTTPS encryption in Java

Source: Internet
Author: User

For the code of the HTTPS access method, copy a usable demo. I have tested it, and I hope it will be useful to you.

 

Import java. Io .*;
Import java.net .*;
Import java. Security .*;
Import java. Security. cert .*;
Import java. util .*;
Import javax.net. SSL .*;

Public class httpstest ...{
// We wowould never hardcode this literal in a real system,
// This is only for this article.
Private string url = "https://www.paypal.com/cn ";

// Create an anonymous class to trust all certificates.
// This is bad style, you should create a separate class.
Private x509trustmanager xtm = new x509trustmanager ()...{
Public void checkclienttrusted (x509certificate [] Chain, string authtype )...{}

Public void checkservertrusted (x509certificate [] Chain, string authtype )...{
System. Out. println ("Cert:" + chain [0]. tostring () + ", authtype:" + authtype );
}

Public x509certificate [] getacceptedissuers ()...{
Return NULL;
}
};

// Create an class to trust all hosts
Private hostnameverifier hnv = new hostnameverifier ()...{
Public Boolean verify (string hostname, sslsession session )...{
System. Out. println ("hostname:" + hostname );
Return true;
}
};

// In this function we configure our system with a less stringent
// Hostname verifier and X509 trust manager. This code is
// Executed once, And callthe static methods of httpsurlconnection
Public httpstest ()...{
// Initialize the TLS sslcontext
// Our trustmanager
Sslcontext = NULL;

Try ...{
Sslcontext = sslcontext. getinstance ("TLS ");
X509trustmanager [] xtmarray = new x509trustmanager []... {xtm };
Sslcontext. INIT (null, xtmarray, new java. Security. securerandom ());
} Catch (generalsecurityexception GSE )...{
// Print out some error message and deal with this exception
}

// Set the default socketfactory and hostnameverifier
// For javax.net. SSL. httpsurlconnection
If (sslcontext! = NULL )...{
Httpsurlconnection. setdefaultsslsocketfactory (sslcontext. getsocketfactory ());
}

Httpsurlconnection. setdefaulthostnameverifier (hnv );
}

// This function is called periodically, the important thing
// To note here is that there is no special code that needs
// Be added to deal with a "HTTPS" url. All of the trust
// Management, verification, is handled by the httpsurlconnection.
Public void run ()...{
Try ...{
Urlconnection urlcon = (new URL (URL). openconnection ();
Bufferedreader in = new bufferedreader (New inputstreamreader (urlcon. getinputstream ()));
String line;

While (line = in. Readline ())! = NULL )...{
System. Out. println (line );
}

// Whatever we want to do with these quotes
} Catch (malformedurlexception mue )...{
Mue. printstacktrace ();
} Catch (ioexception IOE )...{
IOE. printstacktrace ();
} Catch (exception e )...{
E. printstacktrace ();
}
}

Public static void main (string [] ARGs )...{
Httpstest = new httpstest ();
Httpstest. Run ();
}
}
Related Article

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.