Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
HTTP Redirect HandlingExamine HTTP redirects. CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int DECLARE @sTmp0 nvarchar(4000) DECLARE @http int EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int -- Any string unlocks the component for the 1st 30-days. EXEC sp_OAMethod @http, 'UnlockComponent', @success OUT, 'Anything for 30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END DECLARE @url nvarchar(4000) DECLARE @html nvarchar(4000) DECLARE @status int 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 IF @html Is Nothing 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', @iTmp0 OUT PRINT @iTmp0 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', @iTmp0 OUT PRINT @iTmp0 -- 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 = ' + STR(@status) -- Display the complete response header. EXEC sp_OAGetProperty @http, 'LastResponseHeader', @iTmp0 OUT PRINT @iTmp0 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 IF @html Is Nothing 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', @iTmp0 OUT PRINT @iTmp0 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', @iTmp0 OUT PRINT @iTmp0 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 = ' + STR(@status) -- Display the complete response header. EXEC sp_OAGetProperty @http, 'LastResponseHeader', @iTmp0 OUT PRINT @iTmp0 END END GO |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.