[Gambas-user] HttpClient not exposing redirect response codes

T Lee Davidson t.lee.davidson at ...626...
Tue Apr 25 22:29:08 CEST 2017

On 04/25/2017 01:36 PM, Benoît Minisini wrote:
> Le 25/04/2017 à 19:12, T Lee Davidson a écrit :
>> I have run into a couple of sites that are responding with 301 (Moved
>> Permanently) and 302 (Moved Temporarily) codes. However, HttpClient.Code
>> yields "0" for both of those sites/codes (and HttpClient.Reason is
>> empty). 200 & 404 codes are reported successfully.
>> Have I missed something in the properties that is preventing proper
>> disclosure of redirect response codes?
>> Or is this a bug?
>> (Project attached.)
> HttpClient is "just" libcurl, so it may be a libcurl thing (that library
> has gazillons of options). I will check...

Thank you for taking the time to check into that, Benoît.

In the meantime, for anyone else needing to loop through redirects (currently scanning for a "Location" header), here's a short 
example subroutine:

Private Sub TestPage(sUrl As String)

   Dim hClient As New HttpClient
   Dim iMaxRedirects As Integer = 5 'Prevent infinite loop bot trap
   Dim iCurrentRedirects As Integer = 0
   Dim bFound As Boolean

   hClient.UserAgent = "AppleWebKit"
   hClient.Async = False
   hClient.URL = sUrl
   hClient.Timeout = 15
   Inc Application.Busy
     bFound = True 'Reset redirect flag
     If hClient.Headers.Find("Location:*", gb.Like) > -1 Then
       bFound = False 'Redirection
       If iCurrentRedirects = iMaxRedirects Then Break
       Inc iCurrentRedirects
       hClient.URL = Replace(hClient.Headers[hClient.Headers.Find("Location:*", gb.Like)], "Location: ", "")
       Print hClient.URL
       hClient.Headers.Clear 'Must manually clear headers array or "Location" header will persist causing redundant looping
   Loop Until bFound = True
   Dec Application.Busy

   If hClient.Status < 0 Then
     Print "ERROR"
   End If

   Print "Success? -> " & hClient.Code



More information about the User mailing list