This article mainly introduces the URLEncode and URLDecode custom functions implemented in ASP, which are different from the server.urlencode that comes with ASP. Friends in need can refer to it.
When doing a post in Ajax, I found that the data was always garbled when getting it from the server. I looked at some solutions on the Internet and couldn't figure it out. My post was in the form of xml. Because of the garbled code, the server-side xml could not be parsed or made an error. So we encode it before posting, and then decode it on the server side. This solves the problem, but if the data is large, it will probably affect the speed.
Although the request in ASP will automatically decode the URL-encoded string, Request.BinaryRead(Request.TotalBytes) will not decode it when obtaining the post data, so it must be decoded.
The following is the decoding function of the server.urlencode function in ASP that I found
Copy the code code 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
Attached is another encoding function. The difference between this and server.urlencode is that server.urlencode will convert tags such as html or xml, such as
Encoding will also be performed, but the function below will not. I use the following to encode and then decode, because I use xml when using post.
Copy the code code 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