HTTPS is similar to HTTP, except that HTTPS Generally requests the server through post, but HTTPS is different from HTTP in that HTTPS is connected to the server session. HTTP will disconnect the connection after the request is sent.
Send POST request Code :
String query = R4 + "& pass =" + R3; // Request Parameters
Byte [] entitydata = query. getbytes (); // obtain object data
Httpsurlconnection urlcon = (new URL (ticketurl). openconnection ();
Urlcon. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8 ");
Urlcon. setrequestproperty ("Content-Length", String. valueof (entitydata. Length ));
(Httpsurlconnection) urlcon). setrequestmethod ("Post ");
Urlcon. setdooutput (true );
Urlcon. setdoinput (true );
Urlcon. Connect ();
// Send encapsulated object data to the output stream
Outputstream outstream = urlcon. getoutputstream ();
Outstream. Write (entitydata );
Outstream. Flush ();
Outstream. Close ();
// The server returns the input stream and reads and writes it.
Bufferedreader in = new bufferedreader (New inputstreamreader (urlcon. getinputstream ()));
String line;
While (line = in. Readline ())! = NULL ){
Return line;
}
In. Close ();
In addition, the hostnameverifier and x509trustmanager must be implemented when httpsurlconnection is used. These two implementations are required, and security verification exceptions are not reported. Then, initialize sslcontext in x509trustmanager and set the default socketfactory and hostnameverifier for javax.net. SSL. httpsurlconnection. The Code is as follows:
Private myx509trustmanager xtm = new myx509trustmanager ();
Private myhostnameverifier hnv = new myhostnameverifier ();
Public httpsurlconnectiontest (){
// Initialize sslcontext in x509trustmanager
Sslcontext = NULL;
try {
sslcontext = sslcontext. getinstance ("TLS");
x509trustmanager [] xtmarray = new x509trustmanager [] {xtm};
sslcontext. init (null, xtmarray, new Java. security. securerandom ();
}catch (generalsecurityexception GSE) {
}
// Set the default socketfactory and hostnameverifier for javax.net. SSL. httpsurlconnection
If (sslcontext! = NULL ){
Httpsurlconnection. setdefaultsslsocketfactory (sslcontext. getsocketfactory ());
}
Httpsurlconnection. setdefaulthostnameverifier (hnv );
In this way, no error is reported.