Login  

Blog Stats

News


Visual Developer - Visual Basic MVP

隨筆分類

文章分類

每月文章

優質好站連結


強力鎯頭 の VB 部落

您好 ! 歡迎蒞臨 Power Hammer 的 VB 部落 ! 網誌內容主要為 VB .Net C# WMI 等相關資訊 , 提供網友參考

 

如何將ASPServer.URLEncode編碼後的字串解碼還原(URLdecode)

 

' ASP 的寫法 , Function 如下:

 

<%

 

Dim s

 

s=server.urlencode("Hi ! 大家好 , 我是Hammer")

Response.write "URL Encode String : " & s & "<br>"

Response.write "URL Decode String : " & AspUrlDecode(s) & "<p>"

 

s=server.urlencode("強力Power鎯頭Hammer")

Response.write "URL Encode String : " & s & "<br>"

Response.write "URL Decode String : " & AspUrlDecode(s) & "<p>"

 

s=server.urlencode("Power強力Hammer鎯頭")

Response.write "URL Encode String : " & s & "<br>"

Response.write "URL Decode String : " & AspUrlDecode(s) & "<p>"

 

s=server.urlencode("強力鎯頭 PowerHammer")

Response.write "URL Encode String : " & s & "<br>"

Response.write "URL Decode String : " & AspUrlDecode(s) & "<p>"

 

s=server.urlencode("PowerHammer 強力鎯頭")

Response.write "URL Encode String : " & s & "<br>"

Response.write "URL Decode String : " & AspUrlDecode(s) & "<p>"

 

Private Function AspUrlDecode(strValue)

    Dim varAry, varElement, objStream, lngLoop, Flag

    strValue = Replace(strValue, "+", " ")

    varAry = Split(strValue, "%")

    Flag = varAry(0) = ""

    Set objStream = Server.CreateObject("ADODB.Stream")

    With objStream

            .Type = 2

            .Mode = 3

            .Open

            For Each varElement In varAry

                If varElement <> Empty Then

                    If Len(varElement) >= 2 And Flag Then

                        .WriteText ChrB(CInt("&H" & Left(varElement, 2)))

                        For lngLoop = 3 To Len(varElement)

                            .WriteText ChrB(Asc(Mid(varElement, lngLoop, 1)))

                        Next

                    Else

                        For lngLoop = 1 To Len(varElement)

                            .WriteText ChrB(Asc(Mid(varElement, lngLoop, 1)))

                        Next

                        Flag = True

                    End If

                End If

            Next

            .WriteText Chr(0)

            .Position = 0

            AspUrlDecode = Replace(ConvUnicode(.ReadText), Chr(0), "", 1, -1, 0)

            On Error Resume Next

            .Close

            Set objStream = Nothing

    End With

End Function

 

Public Function ConvUnicode(ByVal strData)

    Dim rs, stm, bytAry, intLen

    If Len(strData & "") > 0 Then

        strData = MidB(strData, 1)

        intLen = LenB(strData)

        Set rs = Server.CreateObject("ADODB.Recordset")

        Set stm = Server.CreateObject("ADODB.Stream")

        With rs

            .Fields.Append "X", 205, intLen

            .Open

            .AddNew

            rs(0).AppendChunk strData & ChrB(0)

            .Update

            bytAry = rs(0).GetChunk(intLen)

        End With

        With stm

            .Type = 1

            .Open

            .Write bytAry

            .Position = 0

            .Type = 2

            .Charset = "Big5"

            ConvUnicode = .ReadText

        End With

    End If

    On Error Resume Next

    stm.Close

    Set stm = Nothing

    rs.Close

    Set rs = Nothing

End Function

 

%>

 

 

================================================================

 

 

' VB6 的寫法 , Function 如下:

 

Private Function URLDecode(strValue As String) As String

   

    Dim bytAry() As Byte

    Dim Flag As Boolean

    Dim varAry, varElement

    Dim lngCnt As Long, lngLoop As Long

   

    ReDim bytAry(1 To 999999) As Byte

   

    strValue = Replace(strValue, "+", " ")

    varAry = Split(strValue, "%")

    Flag = varAry(0) = ""

    lngCnt = 1

   

    If UBound(varAry) > 0 Then

        For Each varElement In varAry

            If varElement <> Empty Then

                If Len(varElement) >= 2 And Flag Then

                    bytAry(lngCnt) = Val("&H" & Left(varElement, 2))

                    lngCnt = lngCnt + 1

                    For lngLoop = 3 To Len(varElement)

                        bytAry(lngCnt) = Asc(Mid(varElement, lngLoop, 1))

                        lngCnt = lngCnt + 1

                    Next

                Else

                    For lngLoop = 1 To Len(varElement)

                        bytAry(lngCnt) = Asc(Mid(varElement, lngLoop, 1))

                        lngCnt = lngCnt + 1

                    Next

                    Flag = True

                End If

            End If

        Next

        ReDim Preserve bytAry(1 To lngCnt - 1)

        URLDecode = StrConv(bytAry, vbUnicode)

        Erase bytAry

    Else

        URLDecode = Join(varAry)

    End If

 

End Function

 

' 程式呼叫 UrlDecode 函數 , 傳入 Server.Encode 後的字串 , 將傳回結果顯示於訊息方塊

MsgBox UrlDecode("%B1j%A4O%EE%CF%C0Y%AA%BAVB%B3%A1%B8%A8")

MsgBox UrlDecode("Hi%7E%7E%A8%FE%A8%FE%7E%7EYa%7E%7E%5E%5F%5E%B4N%C2%E6%A4l%7E%7E881")

posted on Tuesday, May 09, 2006 12:19 AM

What People Are Saying About This Post...

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 12/11/2006 9:54 AM 窃听器
OK~

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 12/14/2006 2:43 PM 硬盘数据恢复
不错~

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 6/14/2007 10:00 AM soft903
Very Nice code!!

I look for complete urldecode for a long time (most in web are incomplete)

But a little mistake on the code
when the word is not starting with chinese char or sepcial char
e.g. "s編碼"
i.e. the encoded string is not starting with "%"
it will have error

that go with the code

.WriteText ChrB(CInt("&H" & Left(varElement, 2)))

as the starting string should not be decode , i.e. "s"

so checking of the starting of encoded string is "%" is needed.
if it is not starting with "%", just simple

For lngLoop = 1 To Len(varElement)
.WriteText ChrB(Asc(Mid(varElement, lngLoop, 1)))
Next

Thanks for your help

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 9/19/2007 3:04 PM daisy
程式bug,將I%27m+sandy進行urldecode,第一個字元I,會解析不出

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 9/20/2007 4:08 PM Power Hammer
感謝 soft903 & daisy 的提醒及告知 !
程式已經有調整過囉 !
若還有問題 , 還請不吝指教 , 謝謝 !
^_^



# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 11/15/2007 10:25 AM NANSEN
Good

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 6/20/2008 3:29 PM shinhrn
good??

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 12/30/2008 4:00 PM 搬運公司
學到了 不錯~

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 4/14/2009 12:02 PM william
非常感謝這個啊
太好了
剛好解決我的問題
正好用java 把UTF-8中文存入database( big5 編碼)
因為UTF-8直入 big5 table 讀出來會資料壞掉
所以利用URLencode 寫進去
但asp 卻讀不出來
剛好有你這個 解決了所有問題

順便分享一下
ConvUnicode中如果是 原始資料是UTF-8 在呼叫物件時要改用UTF-8編碼
.Type = 1
.Open
.Write bytAry
.Position = 0
.Type = 2
.Charset = "UTF-8" <--------------這裡
ConvUnicode = .ReadText

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 4/20/2009 12:45 AM Hammer
謝謝您的分享

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 7/15/2009 4:10 PM wow gold
謝謝您的分享 ...

# re: 將ASP Server.URLEncode 編碼後的字串還原(URLdecode) 7/10/2010 4:19 PM Liwei Huang
根本就不用这么麻烦。用下面的方法就可以了。
<script language="JavaScript" type="text/javascript" runat="server">
function URLDecode(str){
return decodeURIComponent(str);
}
</script>

What do you have to say?

Title:
Name:
Url:
驗證碼  
Comments: