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