Sample code for 30+ languages & platforms
Android™

HTTPS Server Certificate Require Hostname Match

See more HTTP Examples

Demonstrates and explains the RequireHostnameMatch property.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The RequireHostnameMatch property was added in Chilkat v11.0.0
    // to ensure the URL's hostname matches at least one of the server certificate SAN's (Subject Alternative Names)
    // 
    // In actuality, it is the SNI hostname that must match.  If the SNI hostname is not explicitly set,
    // then Chilkat uses the hostname from the URL as the SNI hostname.

    // Here's an example using chilkatsoft.com
    // The SSL server certificate for chilkatsoft.com has 2 Subject Alternative Names:
    // 
    // 1) DNS Name: *.chilkatsoft.com
    // 2) DNS Name: chilkatsoft.com
    // 
    // See Explaining the SNI Hostname in TLS

    CkHttp http = new CkHttp();

    http.put_RequireHostnameMatch(true);

    // This should succeed because "www.chilkatsoft.com" matches the SAN entry "*.chilkatsoft.com"
    String html = http.quickGetStr("https://www.chilkatsoft.com/helloWorld.html");
    Log.i(TAG, "1) Succeeded: " + String.valueOf(http.get_LastMethodSuccess()));

    // At the time of writing this example, the IP address for chilkatsoft.com is 3.101.18.47
    // If we send the request using the IP address, it will fail because the IP address is does 
    // not match any of the SAN entries in the server certificate.
    html = http.quickGetStr("https://3.101.18.47/helloWorld.html");
    Log.i(TAG, "2) Succeeded: " + String.valueOf(http.get_LastMethodSuccess()));

    // However, it will succeed if we explicitly set the SNI hostname.
    http.put_SniHostname("www.chilkatsoft.com");
    html = http.quickGetStr("https://3.101.18.47/helloWorld.html");
    Log.i(TAG, "3) Succeeded: " + String.valueOf(http.get_LastMethodSuccess()));

    // Remove our explicit SNI hostname.
    http.put_SniHostname("");

    // Now let's try wrong.host.badssl.com
    // The SSL server certificate for badssl.com has 2 Subject Alternative Names:
    // 
    // 1) DNS Name: *.badssl.com
    // 2) DNS Name: badssl.com

    // The domain wrong.host.badssl.com will fail the RequireHostnameMatch because
    // the wildcarded domain SAN entry only extends 1 level deep.  
    html = http.quickGetStr("https://wrong.host.badssl.com/");
    Log.i(TAG, "4) Succeeded: " + String.valueOf(http.get_LastMethodSuccess()));

    // The expected output is:
    // 1) Succeeded: True
    // 2) Succeeded: False
    // 3) Succeeded: True
    // 4) Succeeded: False

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}