![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) HTTP Redirect HandlingExamine HTTP redirects.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @url nvarchar(4000) DECLARE @html nvarchar(4000) DECLARE @status int -- Please note: The URL used in this example was a valid redirect many years ago, -- but the site does not exist any longer.. SELECT @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: EXEC sp_OASetProperty @http, 'FollowRedirects', 1 -- Send the HTTP GET and return the content in a string. EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, @url EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- On success, LastErrorText will provide information about -- what happened during the call. PRINT '--------------- LastErrorText ------------------' EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 PRINT '------------------------------------------------' -- 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? EXEC sp_OAGetProperty @http, 'WasRedirected', @iTmp0 OUT IF @iTmp0 = 1 BEGIN PRINT 'Chilkat HTTP followed the redirect.' -- Display the final redirect URL: PRINT 'Final URL:' EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @sTmp0 OUT PRINT @sTmp0 -- Note the HTML returned is from the final redirect URL. END ELSE BEGIN PRINT 'Not redirected.' END EXEC sp_OAGetProperty @http, 'LastStatus', @status OUT IF @status = 200 BEGIN PRINT 'status = 200, OK!' END ELSE BEGIN PRINT 'HTTP Response status = ' + @status -- Display the complete response header. EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT PRINT @sTmp0 END -- Now try it without following redirects: PRINT '-------- Now trying without following redirects....' EXEC sp_OASetProperty @http, 'FollowRedirects', 0 -- Send the HTTP GET and return the content in a string. EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, @url EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN -- the HTML string can NULL if a 302 redirect response is received. PRINT 'HTML string returned NULL...' END -- On success, LastErrorText will provide information about -- what happened during the call. PRINT '--------------- LastErrorText ------------------' EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 PRINT '------------------------------------------------' -- 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. EXEC sp_OAGetProperty @http, 'WasRedirected', @iTmp0 OUT IF @iTmp0 = 1 BEGIN PRINT '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... PRINT 'Redirect URL:' EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @sTmp0 OUT PRINT @sTmp0 END ELSE BEGIN PRINT 'Not redirected.' END EXEC sp_OAGetProperty @http, 'LastStatus', @status OUT IF @status = 200 BEGIN PRINT 'status = 200, OK!' END ELSE BEGIN PRINT 'HTTP Response status = ' + @status -- Display the complete response header. EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT PRINT @sTmp0 END EXEC @hr = sp_OADestroy @http END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.