预览模式: 普通 | 列表

vb.net实现url编码解码

VB.net URL解码
ASP/Visual Basic代码
  1. Public Function URLDecode(sEncodedURL As StringAs String  
  2.     On Error Goto Catch  
  3.       
  4.     Dim iLoop As Integer  
  5.     Dim sRtn As String  
  6.     Dim sTmp As String  
  7.       
  8.    
  9.    
  10.     If Len(sEncodedURL) > 0 Then  
  11.    
  12.         For iLoop = 1 To Len(sEncodedURL)  
  13.             sTmp = Mid(sEncodedURL,iLoop,1)  
  14.             sTmp = Replace(sTmp,"+"," ")  
  15.    
  16.             If sTmp = "%" and LEN(sEncodedURL) > iLoop + 2 Then  
  17.                 sTmp = Mid(sEncodedURL,iLoop + 1,2)  
  18.                 sTmp = Chr(CDec("&H" & sTmp))  
  19.                 iLoop = iLoop + 2  
  20.             End If  
  21.             sRtn = sRtn & sTmp  
  22.         Next iLoop  
  23.         URLDecode = sRtn  
  24.     End If  
  25.     Finally:  
  26.     Exit Function  
  27.     Catch:  
  28.     URLDecode = ""  
  29.     Resume Finally  
  30. End Function  
vb.net url编码
ASP/Visual Basic代码
  1. Public Function URLEncode(sRawURL As StringAs String  
  2.     On Error Goto Catch  
  3.     Dim iLoop As Integer  
  4.     Dim sRtn As String  
  5.     Dim sTmp As String  
  6.     Const sValidChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:/.?=_-$(){}~&"  
  7.    
  8.    
  9.     If Len(sRawURL) > 0 Then  
  10.    
  11.    
  12.         For iLoop = 1 To Len(sRawURL)  
  13.             sTmp = Mid(sRawURL,1)  
  14.    
  15.    
  16.             If InStr(1,sValidChars,sTmp,vbBinaryCompare) = 0 Then  
  17.                 sTmp = Hex(Asc(sTmp))  
  18.    
  19.    
  20.                 If sTmp = "20" Then  
  21.                     sTmp = "+"  
  22.                 ElseIf Len(sTmp) = 1 Then  
  23.                     sTmp = "%0" & sTmp  
  24.                 Else  
  25.                     sTmp = "%" & sTmp  
  26.                 End If  
  27.             End If  
  28.             sRtn = sRtn & sTmp  
  29.         Next iLoop  
  30.         URLEncode = sRtn  
  31.     End If  
  32.     Finally:  
  33.     Exit Function  
  34.     Catch:  
  35.     URLEncode = ""  
  36.     Resume Finally  
  37. End Function  
解码测试过没问题,编码没测试。
 

Tags: vb.net

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 102

 广告位

↑返回顶部↑