Perl Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Perl Examples

Quick Start
Perl Unicode
Perl Byte Array
Perl Certs
Perl Email
Perl Encryption
Perl FTP
HTML-to-XML
Perl HTTP
Perl IMAP
Perl MHT
Perl MIME
Perl RSA
Perl S/MIME
Perl Signatures
Perl Socket
Perl Spider
Perl Tar
Perl Upload
Perl XML
Perl XMP
Perl Zip

More Examples...
String
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

HTTP Redirect Handling

Examine HTTP redirects.

Download Chilkat Perl Module

use chilkat;

$http = new chilkat::CkHttp();

#  Any string unlocks the component for the 1st 30-days.
$success = $http->UnlockComponent("Anything for 30-day trial");
if ($success != 1) {
    print $http->lastErrorText() . "\n";
    exit;
}

$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(1);

#  Send the HTTP GET and return the content in a string.
$html = $http->quickGetStr($url);
if ($html eq null ) {
    print $http->lastErrorText() . "\n";
}

#  On success, LastErrorText will provide information about
#  what happened during the call.
print "--------------- LastErrorText ------------------" . "\r\n";
print $http->lastErrorText() . "\r\n";
print "------------------------------------------------" . "\r\n";

#  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() == 1) {
    print "Chilkat HTTP followed the redirect." . "\r\n";

    #  Display the final redirect URL:
    print "Final URL:" . "\r\n";
    print $http->finalRedirectUrl() . "\r\n";

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

}
else {
    print "Not redirected." . "\r\n";
}

$status = $http->get_LastStatus();
if ($status == 200) {
    print "status = 200, OK!" . "\r\n";
}
else {
    print "HTTP Response status = " . $status . "\r\n";

    #  Display the complete response header.
    print $http->lastResponseHeader() . "\r\n";
}

#  Now try it without following redirects:
print "-------- Now trying without following redirects...." . "\r\n";
$http->put_FollowRedirects(0);

#  Send the HTTP GET and return the content in a string.
$html = $http->quickGetStr($url);
if ($html eq null ) {
    #  the HTML string can NULL if a 302 redirect response is received.
    print "HTML string returned NULL..." . "\r\n";
}

#  On success, LastErrorText will provide information about
#  what happened during the call.
print "--------------- LastErrorText ------------------" . "\r\n";
print $http->lastErrorText() . "\r\n";
print "------------------------------------------------" . "\r\n";

#  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() == 1) {
    print "This was a redirect response" . "\r\n";

    #  When redirects are not followed, FinalRedirectUrl
    #  contains the redirect URL that would've been taken...
    #  Display the redirect URL, which was not taken...
    print "Redirect URL:" . "\r\n";
    print $http->finalRedirectUrl() . "\r\n";

}
else {
    print "Not redirected." . "\r\n";
}

$status = $http->get_LastStatus();
if ($status == 200) {
    print "status = 200, OK!" . "\r\n";
}
else {
    print "HTTP Response status = " . $status . "\r\n";

    #  Display the complete response header.
    print $http->lastResponseHeader() . "\r\n";
}

 

Need a specific example? Send a request to support@chilkatsoft.com

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