[點晴永久免費OA].net版URLEncode、ASP版URLDecode,支持漢字編碼/解碼
當(dāng)前位置:點晴教程→點晴OA辦公管理信息系統(tǒng)
→『 經(jīng)驗分享&問題答疑 』
ASP版URLDecode:
function URLDecode(strIn)
URLDecode = ""
Dim sl: sl = 1
Dim tl: tl = 1
Dim key: key = "%"
Dim kl: kl = Len(key)
sl = InStr(sl, strIn, key, 1)
Do While sl>0
If (tl=1 And sl<>1) Or tl<sl Then
URLDecode = URLDecode & Mid(strIn, tl, sl-tl)
End If
Dim hh, hi, hl
Dim a
select Case UCase(Mid(strIn, sl+kl, 1))
Case "U": 'Unicode URLEncode
a = Mid(strIn, sl+kl+1, 4)
URLDecode = URLDecode & ChrW("&H" & a)
sl = sl + 6
Case "E": 'UTF-8 URLEncode
hh = Mid(strIn, sl+kl, 2)
a = Int("&H" & hh) 'ascii碼
If Abs(a)<128 Then
sl = sl + 3
URLDecode = URLDecode & Chr(a)
Else
hi = Mid(strIn, sl+3+kl, 2)
hl = Mid(strIn, sl+6+kl, 2)
a = ("&H" & hh And &H0F) * 2 ^12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
If a<0 Then a = a + 65536
URLDecode = URLDecode & ChrW(a)
sl = sl + 9
End If
Case Else: 'Asc URLEncode
hh = Mid(strIn, sl+kl, 2) '高位
a = Int("&H" & hh) 'ascii碼
If Abs(a)<128 Then
sl = sl + 3
Else
hi = Mid(strIn, sl+3+kl, 2) '低位
a = Int("&H" & hh & hi) '非ascii碼
sl = sl + 6
End If
URLDecode = URLDecode & Chr(a)
End select
tl = sl
sl = InStr(sl, strIn, key, 1)
Loop
URLDecode = URLDecode & Mid(strIn, tl)
End function
.net版URLEncode:
//對網(wǎng)址進行UrlEncode編碼,為確保中文不亂碼,接收后進行URLDecode解碼
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
//對網(wǎng)址進行UrlEncode編碼,為確保中文不亂碼故意將%替換為了$,接收后先將$替換為%,然后再進行URLDecode解碼
public static string UrlEncode_change(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"$" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
} 該文章在 2022/5/5 18:55:55 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |