Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
HTTP Redirect HandlingExamine HTTP redirects. 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; |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.