Delphi Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Delphi Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML-to-XML
HTTP
IMAP
Encryption
MHT / HTML Email
RSA Encryption
S/MIME
Socket
Spider
String
Tar
Upload
XML
XMP
Zip Compression

More Examples...
Byte Array
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
Bzip2
LZW
Bz2
Icon

Type Conversion

 

Article: Understanding COM References in Delphi

HTTP Redirect Handling

Examine HTTP redirects.

Download Chilkat HTTP ActiveX

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls,
    CHILKATHTTPLib_TLB,
    OleCtrls;

...

procedure TForm1.Button1Click(Sender: TObject);
var
http: TChilkatHttp;
success: Integer;
url: String;
html: String;
status: Integer;

begin
http := TChilkatHttp.Create(Self);

//  Any string unlocks the component for the 1st 30-days.
success := http.UnlockComponent('Anything for 30-day trial');
if (success <> 1) then
  begin
    ShowMessage(http.LastErrorText);

  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.FollowRedirects := 1;

//  Send the HTTP GET and return the content in a string.
html := http.QuickGetStr(url);
if (html = NULL ) then
  begin
    ShowMessage(http.LastErrorText);
  end;

//  On success, LastErrorText will provide information about
//  what happened during the call.
Memo1.Lines.Add('--------------- LastErrorText ------------------');
Memo1.Lines.Add(http.LastErrorText);
Memo1.Lines.Add('------------------------------------------------');

//  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.WasRedirected = 1) then
  begin
    Memo1.Lines.Add('Chilkat HTTP followed the redirect.');

    //  Display the final redirect URL:
    Memo1.Lines.Add('Final URL:');
    Memo1.Lines.Add(http.FinalRedirectUrl);

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

  end
else
  begin
    Memo1.Lines.Add('Not redirected.');
  end;

status := http.LastStatus;
if (status = 200) then
  begin
    Memo1.Lines.Add('status = 200, OK!');
  end
else
  begin
    Memo1.Lines.Add('HTTP Response status = '
         + IntToStr(status));

    //  Display the complete response header.
    Memo1.Lines.Add(http.LastResponseHeader);
  end;

//  Now try it without following redirects:
Memo1.Lines.Add('-------- Now trying without following redirects....');
http.FollowRedirects := 0;

//  Send the HTTP GET and return the content in a string.
html := http.QuickGetStr(url);
if (html = NULL ) then
  begin
    //  the HTML string can NULL if a 302 redirect response is received.
    Memo1.Lines.Add('HTML string returned NULL...');
  end;

//  On success, LastErrorText will provide information about
//  what happened during the call.
Memo1.Lines.Add('--------------- LastErrorText ------------------');
Memo1.Lines.Add(http.LastErrorText);
Memo1.Lines.Add('------------------------------------------------');

//  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.WasRedirected = 1) then
  begin
    Memo1.Lines.Add('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...
    Memo1.Lines.Add('Redirect URL:');
    Memo1.Lines.Add(http.FinalRedirectUrl);

  end
else
  begin
    Memo1.Lines.Add('Not redirected.');
  end;

status := http.LastStatus;
if (status = 200) then
  begin
    Memo1.Lines.Add('status = 200, OK!');
  end
else
  begin
    Memo1.Lines.Add('HTTP Response status = '
         + IntToStr(status));

    //  Display the complete response header.
    Memo1.Lines.Add(http.LastResponseHeader);
  end;
end;

 

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

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

Mail Component · .NET Email Component · XML Parser