Establish an android https connection

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.
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 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.