Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Perl Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Perl) HTTP Redirect Handling

Examine HTTP redirects.

Chilkat Perl Downloads

Perl Module for Windows, MacOS, Linux, Alpine Linux, Solaris

use chilkat();

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

$http = chilkat::CkHttp->new();

# Please note: The URL used in this example was a valid redirect many years ago,
# but the site does not exist any longer..
$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 ($http->get_LastMethodSuccess() != 1) {
    print $http->lastErrorText() . "\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/
#     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 ($http->get_LastMethodSuccess() != 1) {
    # 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";
}


 

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