Ruby Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Ruby
Examples

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

More Examples...
String
Email Object
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
DH Key Exchange
DSA

Unreleased...
LZW
Icon

 

 

 

 

 

 

 

HTTP Redirect Handling

Examine HTTP redirects.

Download Chilkat Ruby Library

require 'chilkat'

http = Chilkat::CkHttp.new()

#  Any string unlocks the component for the 1st 30-days.
success = http.UnlockComponent("Anything for 30-day trial")
if (success != true)
    print http.lastErrorText() + "\n"
    exit
end

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 == nil )
    print http.lastErrorText() + "\n"
end

#  On success, LastErrorText will provide information about
#  what happened during the call.
print "--------------- LastErrorText ------------------" + "\n";
print http.lastErrorText() + "\n";
print "------------------------------------------------" + "\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() == true)
    print "Chilkat HTTP followed the redirect." + "\n";

    #  Display the final redirect URL:
    print "Final URL:" + "\n";
    print http.finalRedirectUrl() + "\n";

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

else
    print "Not redirected." + "\n";
end

status = http.get_LastStatus()
if (status == 200)
    print "status = 200, OK!" + "\n";
else
    print "HTTP Response status = " + status.to_s() + "\n";

    #  Display the complete response header.
    print http.lastResponseHeader() + "\n";
end

#  Now try it without following redirects:
print "-------- Now trying without following redirects...." + "\n";
http.put_FollowRedirects(false)

#  Send the HTTP GET and return the content in a string.
html = http.quickGetStr(url)
if (html == nil )
    #  the HTML string can NULL if a 302 redirect response is received.
    print "HTML string returned NULL..." + "\n";
end

#  On success, LastErrorText will provide information about
#  what happened during the call.
print "--------------- LastErrorText ------------------" + "\n";
print http.lastErrorText() + "\n";
print "------------------------------------------------" + "\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() == true)
    print "This was a redirect response" + "\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:" + "\n";
    print http.finalRedirectUrl() + "\n";

else
    print "Not redirected." + "\n";
end

status = http.get_LastStatus()
if (status == 200)
    print "status = 200, OK!" + "\n";
else
    print "HTTP Response status = " + status.to_s() + "\n";

    #  Display the complete response header.
    print http.lastResponseHeader() + "\n";
end

 

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

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