Android communicates with the server over https

Source: Internet
Author: User

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.

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.