Sample code for 30+ languages & platforms
Android™

Bunny Sign URL and then Download using Signed URL

See more Bunny CDN Examples

Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.

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);

    boolean success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    String mySecurityKey = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed";

    String url = "https://test.b-cdn.net/sample-pdf-with-images.pdf";

    // Extract the URL components.
    CkUrl urlObj = new CkUrl();
    urlObj.ParseUrl(url);

    String url_scheme = "https";
    if (urlObj.get_Ssl() == false) {
        url_scheme = "http";
        }

    String url_host = urlObj.host();
    String url_path = urlObj.path();

    // Calculate an expiration time 1 hour from the current date/time.
    CkDateTime expTime = new CkDateTime();
    expTime.SetFromCurrentSystemTime();
    expTime.AddSeconds(3600);
    String expires = expTime.getAsUnixTimeStr(false);

    Log.i(TAG, "Expires = " + expires);

    // Create the string to hash
    CkStringBuilder sbToHash = new CkStringBuilder();
    sbToHash.Append(mySecurityKey);
    sbToHash.Append(url_path);
    sbToHash.Append(expires);

    // Base64Url encoding is the same as base64, except "-" is used instead of "+",
    // "_" is used instead of "/", and no "=" padding is added.
    String token = sbToHash.getHash("sha256","base64Url","utf-8");

    CkStringBuilder sbSignedUrl = new CkStringBuilder();
    sbSignedUrl.Append(url_scheme);
    sbSignedUrl.Append("://");
    sbSignedUrl.Append(url_host);
    sbSignedUrl.Append(url_path);
    sbSignedUrl.Append("?token=");
    sbSignedUrl.Append(token);
    sbSignedUrl.Append("&expires=");
    sbSignedUrl.Append(expires);

    String signedUrl = sbSignedUrl.getAsString();
    Log.i(TAG, "Signed URL: " + signedUrl);

    // Use the signed URL to download the file.
    CkHttp http = new CkHttp();
    success = http.Download(signedUrl,"c:/aaworkarea/sample.pdf");
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        }
    else {
        Log.i(TAG, "Success.");
        }


  }

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