Java Examples

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

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
SFTP
Signatures
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
Upload
XML
XMP
Zip

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

HTTP Redirect Handling

Examine HTTP redirects.

 Chilkat Java Library Downloads for Windows, Linux, and MAC OS X

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) {
        System.out.println(http.lastErrorText());
        return;
    }

    String url;
    String html;
    int status;

    url = "http://www.planyourweddingonline.co.za/";

    //  The FollowRedirects property controls whether redirects
    //  are automatically followed.  The default behavior is to
    //  automatically follow redirects.

    //  Explicitly set FollowRedirects so that redirects are automatically taken:
    http.put_FollowRedirects(true);

    //  Send the HTTP GET and return the content in a string.
    html = http.quickGetStr(url);
    if (html == null ) {
        System.out.println(http.lastErrorText());
    }

    //  On success, LastErrorText will provide information about
    //  what happened during the call.
    System.out.println("--------------- LastErrorText ------------------");
    System.out.println(http.lastErrorText());
    System.out.println("------------------------------------------------");

    //  In this case, we see something like this:
    //  ChilkatLog:
    //    QuickGetHtml:
    //      DllDate: Jul 27 2007
    //      url: http://www.planyourweddingonline.co.za/
    //      httpServer: www.planyourweddingonline.co.za
    //      port: 80
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: main/main/home/index.php
    //      url: http://www.planyourweddingonline.co.za/main/main/home/index.php
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: /main/main/home/index.php?SMC=1
    //      url: http://www.planyourweddingonline.co.za/main/main/home/index.php?SMC=1
    //      StatusCode: 200
    //      StatusText: OK
    //      CompressedSize: 7434
    //      UncompressedSize: 40999

    //  Was the GET redirected?
    if (http.get_WasRedirected() == true) {
        System.out.println("Chilkat HTTP followed the redirect.");

        //  Display the final redirect URL:
        System.out.println("Final URL:");
        System.out.println(http.finalRedirectUrl());

        //  Note the HTML returned is from the final redirect URL.

    }
    else {
        System.out.println("Not redirected.");
    }

    status = http.get_LastStatus();
    if (status == 200) {
        System.out.println("status = 200, OK!");
    }
    else {
        System.out.println("HTTP Response status = "
             + status);

        //  Display the complete response header.
        System.out.println(http.lastResponseHeader());
    }

    //  Now try it without following redirects:
    System.out.println("-------- Now trying without following redirects....");
    http.put_FollowRedirects(false);

    //  Send the HTTP GET and return the content in a string.
    html = http.quickGetStr(url);
    if (html == null ) {
        //  the HTML string can NULL if a 302 redirect response is received.
        System.out.println("HTML string returned NULL...");
    }

    //  On success, LastErrorText will provide information about
    //  what happened during the call.
    System.out.println("--------------- LastErrorText ------------------");
    System.out.println(http.lastErrorText());
    System.out.println("------------------------------------------------");

    //  In this case, we see something like this:
    //  ChilkatLog:
    //    QuickGetHtml:
    //      DllDate: Jul 27 2007
    //      url: http://www.planyourweddingonline.co.za/
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: main/main/home/index.php

    //  Was this a redirect?  Even if FollowRedirects is false,
    //  WasRedirected will be true (non-zero) if the response
    //  indicated a redirect.
    if (http.get_WasRedirected() == true) {
        System.out.println("This was a redirect response");

        //  When redirects are not followed, FinalRedirectUrl
        //  contains the redirect URL that would've been taken...
        //  Display the redirect URL, which was not taken...
        System.out.println("Redirect URL:");
        System.out.println(http.finalRedirectUrl());

    }
    else {
        System.out.println("Not redirected.");
    }

    status = http.get_LastStatus();
    if (status == 200) {
        System.out.println("status = 200, OK!");
    }
    else {
        System.out.println("HTTP Response status = "
             + status);

        //  Display the complete response header.
        System.out.println(http.lastResponseHeader());
    }

  }
}

 

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