Skip to content
pyricau edited this page Jun 7, 2012 · 4 revisions

@HttpsClient

The @HttpsClient Simplify HTTPS requests by configuring the KeyStore, TrustStore and Hostname Verification.

All the parameters are optionals

Usage examples

Mutual SSL authentication (two-way authentication)

This is the complete form if you want to achieve Client Auth

@HttpsClient(trustStore=R.raw.cacerts, 
	     trustStorePwd="changeit",
	     keyStore=R.raw.client,
	     keyStorePwd="secret",
	     hostnameVerif=true)
HttpClient mHttps;
  • trustStore: int, Resource id of your trust store file ex R.raw.cacerts.bks Typically your servers trusted certificates (public key, Root Chain Authority etc)

  • trustStorePwd: String, Your TrustStore password (default is changeit)

  • keyStore: int, Resource id of your keystore Usually your private key (client certificate)

  • keyStorePwd: Your KeyStore password (default is changeit)

  • hostnameVerif: boolean, Turn on or off strict hostname verification (default false) Hostname in certificate (DN) must match the URL

Note: Until ICS, Android accept [Key|Trust]store only in BKS format (Bouncycastle Key Store)

A simple SSL authentication

(this is useful if your remote server use a selfsigned certificate or a certificate issued by a private certificate authority)

@HttpsClient(trustStore=R.raw.mycacerts, 
	     trustStorePwd="changeit")
HttpClient mHttps;

Default

If you don't specify a truststore file, the annotation will use the default android truststore located at /system/etc/security/cacerts.bks it allow you to connect to a server signed with one of the trusted root CAs (Thawte, verisign etc.)

@HttpsClient
HttpClient mHttps;

Complete Example

@EActivity(R.layout.main)
public class HttpsClientSampleActivity extends Activity {

	@HttpsClient(trustStore=R.raw.cacerts,
                     trustStorePwd="changeit", 
                     hostnameVerif=true)
	HttpClient mHttps;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        secureConnection();
    }
    
    @Background
    public void secureConnection () {
    	try {
	    	HttpGet httpget = new HttpGet("https://www.verisign.com/");
  	    	HttpResponse response = mHttps.execute(httpget);
	    	
	    	doSomethingWithResponse(response);
	    	
    	} catch (Exception e) {
	      e.printStackTrace();
	}
    }
    
    @UiThread
    public void doSomethingWithResponse (HttpResponse resp) {
    	Toast.makeText(this, "HTTP status " + resp.getStatusLine().getStatusCode(), Toast.LENGTH_LONG).show();
    }
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally