SoFunction
Updated on 2025-03-10

URLEncode and URLDecode custom functions implemented in ASP

When doing post in ajax, I found that when data is obtained on the server side, I always have garbled code. I couldn't solve some solutions online. I used to post in the past in XML form, but because of garbled code, the xml on the server side could not be parsed or errored. So I encode it before posting, and then decode it on the server side. This problem is solved, but if the data is large, it will probably affect the speed.

Although the request in ASP will automatically decode the url-encoded string, it will not decode when () obtains the post data, so it needs to be decoded.

Below is a decoding function of a function in an ASP I found

Copy the codeThe code is as follows:

Function URLDecode(enStr)
  dim deStr,strSpecial
  dim c,i,v
    deStr=""
    strSpecial="!""#$%&'()*+,.-_/:;< =>?@[/]^`{|}~%"
    for i=1 to len(enStr)
      c=Mid(enStr,i,1)
      if c="%" then
        v=eval("&h"+Mid(enStr,i+1,2))
        if inStr(strSpecial,chr(v))>0 then
          deStr=deStr&chr(v)
          i=i+2
        else
          v=eval("&h"+ Mid(enStr,i+1,2) + Mid(enStr,i+4,2))
          deStr=deStr & chr(v)
          i=i+5
        end if
      else
        if c="+" then
          deStr=deStr&" "
        else
          deStr=deStr&c
        end if
      end if
    next
    URLDecode=deStr
End function

Another encoding function is attached. The difference between this is: it will use html or xml and other tags, such as
It will also be encoded, but the following function will not. I used the following to encode and then decode because I used xml when using post.

Copy the codeThe code is as follows:

private Function URLEncoding(vstrIn)
    strReturn = ""
    For i = 1 To Len(vstrIn)
    ThisChr = Mid(vStrIn,i,1)
    If Abs(Asc(ThisChr)) < &HFF Then
    strReturn = strReturn & ThisChr
    Else
    innerCode = Asc(ThisChr)
    If innerCode < 0 Then
    innerCode = innerCode + &H10000
    End If
    Hight8 = (innerCode And &HFF00)/ &HFF
    Low8 = innerCode And &HFF
    strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
    End If
    Next
    URLEncoding = strReturn
  End Function