VB API URL编码 URL解码

Option Explicit
Public Const MAX_PATH                   As Long = 260
Public Const ERROR_SUCCESS              As Long = 0

'将整个URL参数作为一个URL段
Public Const URL_ESCAPE_SEGMENT_ONLY    As Long = &H2000
Public Const URL_ESCAPE_PERCENT         As Long = &H1000
Public Const URL_UNESCAPE_INPLACE       As Long = &H100000

'路径中包含#
Public Const URL_INTERNAL_PATH          As Long = &H800000
Public Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000
Public Const URL_ESCAPE_SPACES_ONLY     As Long = &H4000000
Public Const URL_DONT_SIMPLIFY          As Long = &H8000000

'转换不安全字符为相应的退格序列
Public Declare Function UrlEscape Lib "shlwapi" _
    Alias "UrlEscapeA" _
    (ByVal pszURL As String, _
    ByVal pszEscaped As String, _
    pcchEscaped As Long, _
    ByVal dwFlags As Long) As Long
    'Download by http://www.codefans.net
    '转换退格序列为普通的字符
Public Declare Function UrlUnescape Lib "shlwapi" _
    Alias "UrlUnescapeA" _
    (ByVal pszURL As String, _
    ByVal pszUnescaped As String, _
    pcchUnescaped As Long, _
    ByVal dwFlags As Long) As Long


Public Function EncodeUrl(ByVal sUrl As String) As String
    Dim sUrlEsc As String
    Dim dwSize As Long
    Dim dwFlags As Long
    If Len(sUrl) > 0 Then
        sUrlEsc = Space$(MAX_PATH)
        dwSize = Len(sUrlEsc)
        dwFlags = URL_DONT_SIMPLIFY
        If UrlEscape(sUrl, _
            sUrlEsc, _
            dwSize, _
            dwFlags) = ERROR_SUCCESS Then
            EncodeUrl = Left$(sUrlEsc, dwSize)
        End If                                                                  'If UrlEscape
    End If                                                                      'If Len(sUrl) > 0
End Function

Public Function DecodeUrl(ByVal sUrl As String) As String
    Dim sUrlUnEsc As String
    Dim dwSize As Long
    Dim dwFlags As Long
    If Len(sUrl) > 0 Then
        sUrlUnEsc = Space$(MAX_PATH)
        dwSize = Len(sUrlUnEsc)
        dwFlags = URL_DONT_SIMPLIFY
        If UrlUnescape(sUrl, _
            sUrlUnEsc, _
            dwSize, _
            dwFlags) = ERROR_SUCCESS Then
            DecodeUrl = Left$(sUrlUnEsc, dwSize)
        End If                                                                  'If UrlUnescape
    End If                                                                      'If Len(sUrl) > 0
End Function
'
'出处不明.



转载于:https://www.cnblogs.com/SilenceRet/archive/2011/11/03/6805039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值