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. Dim http As New ChilkatHttp Dim success As Long ' Any string unlocks the component for the 1st 30-days. success = http.UnlockComponent("Anything for 30-day trial") If (success <> 1) Then MsgBox http.LastErrorText Exit Sub End If Dim url As String Dim html As String Dim status As Long 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 = vbNullString ) Then MsgBox http.LastErrorText End If ' On success, LastErrorText will provide information about ' what happened during the call. Text1.Text = Text1.Text & "--------------- LastErrorText ------------------" & vbCrLf Text1.Refresh Text1.Text = Text1.Text & http.LastErrorText & vbCrLf Text1.Refresh Text1.Text = Text1.Text & "------------------------------------------------" & vbCrLf Text1.Refresh ' 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 Text1.Text = Text1.Text & "Chilkat HTTP followed the redirect." & vbCrLf Text1.Refresh ' Display the final redirect URL: Text1.Text = Text1.Text & "Final URL:" & vbCrLf Text1.Refresh Text1.Text = Text1.Text & http.FinalRedirectUrl & vbCrLf Text1.Refresh ' Note the HTML returned is from the final redirect URL. Else Text1.Text = Text1.Text & "Not redirected." & vbCrLf Text1.Refresh End If status = http.LastStatus If (status = 200) Then Text1.Text = Text1.Text & "status = 200, OK!" & vbCrLf Text1.Refresh Else Text1.Text = Text1.Text & "HTTP Response status = " _ & status & vbCrLf Text1.Refresh ' Display the complete response header. Text1.Text = Text1.Text & http.LastResponseHeader & vbCrLf Text1.Refresh End If ' Now try it without following redirects: Text1.Text = Text1.Text & "-------- Now trying without following redirects...." & vbCrLf Text1.Refresh http.FollowRedirects = 0 ' Send the HTTP GET and return the content in a string. html = http.QuickGetStr(url) If (html = vbNullString ) Then ' the HTML string can NULL if a 302 redirect response is received. Text1.Text = Text1.Text & "HTML string returned NULL..." & vbCrLf Text1.Refresh End If ' On success, LastErrorText will provide information about ' what happened during the call. Text1.Text = Text1.Text & "--------------- LastErrorText ------------------" & vbCrLf Text1.Refresh Text1.Text = Text1.Text & http.LastErrorText & vbCrLf Text1.Refresh Text1.Text = Text1.Text & "------------------------------------------------" & vbCrLf Text1.Refresh ' 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 Text1.Text = Text1.Text & "This was a redirect response" & vbCrLf Text1.Refresh ' When redirects are not followed, FinalRedirectUrl ' contains the redirect URL that would've been taken... ' Display the redirect URL, which was not taken... Text1.Text = Text1.Text & "Redirect URL:" & vbCrLf Text1.Refresh Text1.Text = Text1.Text & http.FinalRedirectUrl & vbCrLf Text1.Refresh Else Text1.Text = Text1.Text & "Not redirected." & vbCrLf Text1.Refresh End If status = http.LastStatus If (status = 200) Then Text1.Text = Text1.Text & "status = 200, OK!" & vbCrLf Text1.Refresh Else Text1.Text = Text1.Text & "HTTP Response status = " _ & status & vbCrLf Text1.Refresh ' Display the complete response header. Text1.Text = Text1.Text & http.LastResponseHeader & vbCrLf Text1.Refresh End If |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.