程序员

ASP编码和解码函数详解

作者:admin 2021-04-15 我要评论

用ASP开发的时候遇到一个解码问题。虽然在ASP中使用Request获取编码过URL字符串会自动解码,但是Request.BinaryRead(Request.TotalBytes)取得Post数据时却不会解...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

用ASP开发的时候遇到一个解码问题。虽然在ASP中使用Request获取编码过URL字符串会自动解码,但是Request.BinaryRead(Request.TotalBytes)取得Post数据时却不会解码,所以只能手动进行解码。
ASP解码函数:

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

只是个人爱好,自己研究了一下编码的实现思路,最后自己写了一个编码函数,提供大家参考。注:ASP有内置的编码函数,即是Server.URLEncode。

ASP编码函数:

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

建议大家在中文编码的时候,还是使用ASP 内置的函数。虽然上面这个编码函数测试过N 遍了,没有发现问题,但是以防万一存在Bug。

以上就是关于ASP编码和解码函数,希望对大家的学习有所帮助。


原文链接:https://m.jb51.net/article/73047.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库