Search code examples
urlencodevisual-foxprofoxpro

URLEncode in FoxPro


I couldn't quickly find a google answer, and I don't have the help in front of me (long story), so, my question is: Is there an easy way to URL-encode a string in MS Visual FoxPro 8


Solution

  • Here's a pure FoxPro function that does what you need:

        ********************************************************
        FUNCTION URLEncode
        *******************
        ***  Function: Encodes a string in URL encoded format
        ***            for use on URL strings or when passing a
        ***            POST buffer to wwIPStuff::HTTPGetEx
        ***      Pass: tcValue  -   String to encode
        ***    Return: URLEncoded string or ""
        ********************************************************
        LPARAMETER tcValue
        LOCAL lcResult, lcChar, lnSize, lnX
    
        *** Large Buffers use the wwIPStuff function 
        *** for quicker response
        if  LEN(tcValue) > 512
           lnSize=LEN(tcValue)
           tcValue=PADR(tcValue,lnSize * 3)
    
           DECLARE INTEGER VFPURLEncode ;
              IN WWIPSTUFF ;
              STRING @cText,;
              INTEGER cInputTextSize
    
           lnSize=VFPUrlEncode(@tcValue,lnSize)
    
           IF lnSize > 0
              RETURN SUBSTR(TRIM(tcValue),1,lnSize)
           ENDIF
           RETURN ""
        ENDIF   
    
        *** Do it in VFP Code
        lcResult=""
    
        FOR lnX=1 to len(tcValue)
           lcChar = SUBSTR(tcValue,lnX,1)
           DO CASE
           CASE ATC(lcChar,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~") > 0
              lcResult=lcResult + lcChar
           CASE lcChar=" "
              lcResult = lcResult + "%20"
           OTHERWISE
               *** Convert others to Hex equivalents
               lcResult = lcResult + "%" + RIGHT(transform(ASC(lcChar),"@0"),2)
           ENDCASE
        ENDFOR && lnX=1 to len(tcValue)
    
        RETURN lcResult
        * EOF URLEncode
    

    Note it has a fallback for wwipstuff.dll on larger buffers - if you're using any West Wind tools (West Wind Client Tools or WebConnection) you'll have this DLL if not just remove that block of code. For larger buffers (say encoding POST buffers) the Fox code routine can get slow hence the fallback to a DLL to provide this functionality.

    Another option if you only encode URLs (rather than POST buffers HTTP POST operations) you can use the InternetCanonicalizeUrl API in Windows:

    *******************************************
    FUNCTION UrlEncode(lcUrl)
    *************************
    DECLARE INTEGER InternetCanonicalizeUrl IN WinInet.DLL ;
       STRING url, STRING @buffer, INTEGER @size, intEGer flags 
    
    lnSize = LEN(lcUrl) *3
    lcBuffer = REPL(" ",lnSize)
    lnSize = LEN(lcBuffer)
    lnFlags = 0 &&ICU_ENCODE_PERCENT 
    if( InternetCanonicalizeUrl(lcUrl,@lcBuffer,@lnSize, lnFlags) == 0)
        RETURN ""
    ENDIF   
    
    IF lnSize > 0
       RETURN LEFT(lcBuffer,lnSize)
    ENDIF
    
    RETURN ""
    

    This code is fast but it's very lenient in its character encoding. Basically it doesn't encode any extended characters which may or may not be important for you. Use this if you really are only encoding URLs. If you are UrlEncoding HTTP Form variables use the routine above.