Android Programming Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Android™ Examples

Bounced Email
Digital Certificates
Digital Signatures
DSA
Email Object
Encryption
FTP
HTML-to-XML
HTTP
IMAP
MHT / HTML Email
POP3
RSA
MIME
SMTP
Socket
SOCKS Proxy
Spider
SSH Key
SSH
SFTP
Tar
Upload
XML
XMP
Zip


More Examples...
Amazon S3
NTLM
RSS
Atom
PPMD
Deflate
Bzip2
LZW
Diffie-Hellman
Bz2
Character Encoding
CSV

 

 

 

 

 

 

 

 

(Chilkat for Android™ API)
Adding Cookies to an HTTP Request

Demonstrates how to add one or more cookies to an HTTP request. For Chilkat HTTP methods that use an HTTP request object, such as SynchronousRequest or PostUrlEncoded, cookies are added by calling the AddHeader method on the request object. For Chilkat HTTP methods that do not use an HTTP request object, such as QuickGetStr, cookies are added by calling AddQuickHeader.

Download: Chilkat for Android™ Java Libraries

// 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 {
  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    String outStr = "";

    CkHttp http = new CkHttp();

    boolean success;

    //  Any string unlocks the component for the 1st 30-days.
    success = http.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        outStr += http.lastErrorText() + "\n";
        tv.setText(outStr);
        setContentView(tv);
        return;
    }

    //  The Cookie header field has this format:
    //  Cookie: name1=value1 [; name2=value2] ...

    //  Build an HTTP POST request:
    CkHttpRequest request = new CkHttpRequest();
    request.SetFromUrl("http://www.chilkatsoft.com/echoPost.asp");
    request.put_HttpVerb("POST");

    request.AddParam("param1","value1");
    request.AddParam("param2","value2");

    //  To add cookies to any HTTP request sent by a Chilkat HTTP method
    //  that uses an HTTP request object, add the cookies to the
    //  request object by calling AddHeader.

    //  Add two cookies:
    request.AddHeader("Cookie","user=\"mary\"; city=\"Chicago\"");

    //  Send the HTTP POST.
    //  (The cookies are sent as part of the HTTP header.)

    CkHttpResponse response;
    String domain;
    domain = "www.chilkatsoft.com";
    int port;
    port = 80;
    boolean ssl;
    ssl = false;
    response = http.SynchronousRequest(domain,port,ssl,request);
    if (response == null ) {
        outStr += http.lastErrorText() + "\n";
        tv.setText(outStr);
        setContentView(tv);
        return;
    }

    //  Display the HTML body of the response.
    if (response.get_StatusCode() == 200) {
        //  Show the last HTTP request header sent, which should include
        //  our cookies...
        outStr += http.lastHeader() + "\n";
    }
    else {
        outStr += "HTTP Response Status = " + response.get_StatusCode() + "\n";
    }

    outStr += "---------------------" + "\n";

    //  Some Chilkat HTTP methods do not use an HTTP request object.
    //  For these methods, such as for QuickGetStr, cookies (or any HTTP request header)
    //  are added by calling AddQuickHeader.
    http.AddQuickHeader("Cookie","user=\"mary\"; city=\"Chicago\"");

    String html;
    html = http.quickGetStr("http://www.w3.org/");
    if (html == null ) {
        outStr += http.lastErrorText() + "\n";
    }
    else {
        //  Show the last HTTP request header sent, which should include
        //  our cookies...
        outStr += http.lastHeader() + "\n";
    }

    tv.setText(outStr);
    setContentView(tv);
  }

  static {
      // Important: Make sure the name passed to loadLibrary matches the shared library
      // found in your project's libs/armeabi directory.
      //  for "libchilkat.so", pass "chilkat" to loadLibrary
      //  for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
      //  etc.
      // 
      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."
  }
}

© 2000-2012 Chilkat Software, Inc. All Rights Reserved.