Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Tracing HTTP RedirectsExample to trace through HTTP redirects. CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr 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 NOT automatically taken: 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 NULL BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END EXEC sp_OAGetProperty @http, 'LastStatus', @status OUT PRINT 'HTTP Response Status: ' + STR(@status) -- The FinalRedirectUrl property will contain the redirect URL -- If FollowRedirects was equal to 1, then all of the -- intermediate redirects (if any) would be followed until -- there were no more redirects. However, because -- FollowRedirects is not 1, FinalRedirectUrl contains -- the next redirect URL. EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @sTmp0 OUT PRINT 'Redirect URL: ' + @sTmp0 DECLARE @loopCount int SELECT @loopCount = 0 WHILE (@status = 302) BEGIN EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @url OUT EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, @url IF @html Is NULL BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 BREAK END EXEC sp_OAGetProperty @http, 'LastStatus', @status OUT EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @sTmp0 OUT PRINT 'Redirect URL: ' + @sTmp0 -- You may wish to check that if FinalRedirectUrl -- equals the URL just retrieved. If so, then break out of the -- loop. (This check is omitted in this example.) -- Instead, this example will prevent infinite loops by -- keeping a loopCount and only allows following a max -- of 10 redirects: SELECT @loopCount = @loopCount + 1 IF @loopCount > 10 BEGIN BREAK END END END GO |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.