/** Creates the Activity and registers a MemorizingTrustManager. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.mtmexample); // set up gui elements findViewById(R.id.connect).setOnClickListener(this); content = (TextView) findViewById(R.id.content); urlinput = (EditText) findViewById(R.id.url); verifyhost = (CheckBox) findViewById(R.id.verifyhost); // register handler for background thread hdlr = new Handler(); // Here, the MemorizingTrustManager is activated for HTTPS try { // set location of the keystore MemorizingTrustManager.setKeyStoreFile("private", "sslkeys.bks"); // register MemorizingTrustManager for HTTPS SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); defaultverifier = HttpsURLConnection.getDefaultHostnameVerifier(); // disable redirects to reduce possible confusion HttpsURLConnection.setFollowRedirects(false); } catch (Exception e) { e.printStackTrace(); } }
@SuppressWarnings("static-access") public boolean open(String method, String url, String charset) { try { URL __url = new URL(url); if (url.toLowerCase().startsWith("https://")) { HttpsHandler handler = new HttpsHandler(); handler.trustAllHttpsCertificates(); HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(hv); HttpsURLConnection https; https = (HttpsURLConnection) __url.openConnection(); https.setDoInput(true); https.setDoOutput(true); https.setRequestMethod(method); https.setFollowRedirects(true); // https.setRequestProperty("Cookie",cookie); } else { HttpURLConnection http = null; http = (HttpURLConnection) __url.openConnection(); http.setDoInput(true); http.setDoOutput(true); http.setRequestMethod(method); http.setFollowRedirects(true); // http.setRequestProperty("Cookie",cookie); } } catch (Exception e) { return false; } return true; }