Sample code for 30+ languages & platforms
DataFlex

Manually Follow HTTP Redirects

See more HTTP Examples

Demonstrates how to manually follow redirects for an HTTP GET.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Integer iStatus
    Integer iLoopCount
    String sNextUrl
    String sTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // 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:
    Set ComFollowRedirects Of hoHttp To False

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" "http://yandex.ru/" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iStatus
    Showln "HTTP Response Status: " iStatus

    // The FinalRedirectUrl property will contain the redirect URL
    // If FollowRedirects was equal to True, then all of the 
    // intermediate redirects (if any) would be followed until
    // there were no more redirects.  However, because 
    // FollowRedirects is not True, FinalRedirectUrl contains
    // the next redirect URL.

    Move 0 To iLoopCount

    While (iStatus = 302)

        Get ComFinalRedirectUrl Of hoHttp To sTemp1
        Showln "Redirect URL: " sTemp1

        Get ComFinalRedirectUrl Of hoHttp To sNextUrl
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" sNextUrl vResp To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoHttp To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComStatusCode Of hoResp To iStatus
        Showln "HTTP Response Status: " iStatus

        // For safety, prevent infinite loops by
        // keeping a loopCount and only allows following a max 
        // of 10 redirects:
        Move (iLoopCount + 1) To iLoopCount
        If (iLoopCount > 10) Begin
            Showln "Too many redirects."
            Procedure_Return
        End

    Loop



End_Procedure