Set up HTTPS Connection - Android Network

Android examples for Network:HTTP

Description

Set up HTTPS Connection

Demo Code

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.SecureRandom;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.net.wifi.WifiManager;

public class Main {
  private static final String USER_AGENT = "TopAndroidSDK";

  private static HttpURLConnection getConnection(Context context, URL url,
      String method, String ctype) throws IOException {
    Proxy proxy = null;//from  w  w w .  j  a  v  a 2  s.  c  o m
    WifiManager wifiManager = (WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {

      Uri uri = Uri.parse("content://telephony/carriers/preferapn");
      Cursor mCursor = context.getContentResolver().query(uri, null, null,
          null, null);
      if (mCursor != null && mCursor.moveToFirst()) {

        String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
        if (proxyStr != null && proxyStr.trim().length() > 0) {
          proxy = new Proxy(Proxy.Type.HTTP,
              new InetSocketAddress(proxyStr, 80));
        }
        mCursor.close();
      }
    }

    HttpURLConnection conn = null;
    if ("https".equals(url.getProtocol())) {
      SSLContext ctx = null;
      try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0],
            new TrustManager[] { new DefaultTrustManager() },
            new SecureRandom());
      } catch (Exception e) {
        throw new IOException(e.getMessage());
      }
      HttpsURLConnection connHttps = null;
      if (proxy == null) {
        connHttps = (HttpsURLConnection) url.openConnection();
      } else {
        connHttps = (HttpsURLConnection) url.openConnection(proxy);
      }
      connHttps.setSSLSocketFactory(ctx.getSocketFactory());
      connHttps.setHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      });
      conn = connHttps;
    } else {
      if (proxy == null) {
        conn = (HttpURLConnection) url.openConnection();
      } else {
        conn = (HttpURLConnection) url.openConnection(proxy);
      }
    }

    conn.setRequestMethod(method);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Content-Type", ctype);
    conn.setRequestProperty("Accept-Encoding", "gzip");// support gzip
    return conn;
  }
}

Related Tutorials