欢迎光临:
  
  非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源) 智能多路控制(串口编程开关) 带源码!


05-06
03

URL解码\编码函数

解码一个编码后的URL函数:

Public Function URLDecode(sEncodedURL As String) As String
    On Error Goto Catch
    
    Dim iLoop As Integer
    Dim sRtn As String
    Dim sTmp As String
    


    If Len(sEncodedURL) > 0 Then

        For iLoop = 1 To Len(sEncodedURL)
            sTmp = Mid(sEncodedURL, iLoop, 1)
            sTmp = Replace(sTmp, "+", " ")

            If sTmp = "%" and LEN(sEncodedURL) > iLoop + 2 Then
                sTmp = Mid(sEncodedURL, iLoop + 1, 2)
                sTmp = Chr(CDec("&H" & sTmp))
                iLoop = iLoop + 2
            End If
            sRtn = sRtn & sTmp
        Next iLoop
        URLDecode = sRtn
    End If
    Finally:
    Exit Function
    Catch:
    URLDecode = ""
    Resume Finally
End Function


顺便发布对应编码的函数:

Public Function URLEncode(sRawURL As String) As String
    On Error Goto Catch
    Dim iLoop As Integer
    Dim sRtn As String
    Dim sTmp As String
    Const sValidChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:/.?=_-$(){}~&"


    If Len(sRawURL) > 0 Then


        For iLoop = 1 To Len(sRawURL)
            sTmp = Mid(sRawURL, iLoop, 1)


            If InStr(1, sValidChars, sTmp, vbBinaryCompare) = 0 Then
                sTmp = Hex(Asc(sTmp))


                If sTmp = "20" Then
                    sTmp = "+"
                ElseIf Len(sTmp) = 1 Then
                    sTmp = "%0" & sTmp
                Else
                    sTmp = "%" & sTmp
                End If
            End If
            sRtn = sRtn & sTmp
        Next iLoop
        URLEncode = sRtn
    End If
    Finally:
    Exit Function
    Catch:
    URLEncode = ""
    Resume Finally
End Function


相关日志:
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 3 | 引用: 0 | 查看次数: 5574

回复回复gaby [2007-06-23 10:25:44 |  | del]
这个代码不咋样 不支持中文url编码
回复回复Noctune [2006-03-28 11:29:47 |  | del]
If InStr(1, sValidChars, sTmp, vbBinaryCompare) = 0
应该是
If InStr(1, sValidChars, sTmp, vbBinaryCompare) > 0
吧?
回复回复grief [2005-06-04 00:51:44 |  | del]
多谢,站长效率真是高呀。
发表评论
您没有权限发表评论!