java asp分析各种搜索引擎的关键字,自动识别url 中(2)


try {
return URLDecoder.decode(keywordsTmp, encodeString);
} catch (UnsupportedEncodingException e) {
return "";
}
}
return "";
}
public static String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
}

以下是Asp的实现代码:
复制代码 代码如下:

Function DecodeURI(s)
s = UnEscape(s)
Dim reg, cs
cs = "GBK"
Set reg = New RegExp
reg.Pattern = "^(?:[\x00-\x7f]|[\xfc-\xff][\x80-\xbf]{5}|[\xf8-\xfb][\x80-\xbf]{4}|[\xf0-\xf7][\x80-\xbf]{3}|[\xe0-\xef][\x80-\xbf]{2}|[\xc0-\xdf][\x80-\xbf])+$"
If reg.Test(s) Then cs = "UTF-8"
Set reg = Nothing
Dim sm
Set sm = CreateObject("ADODB.Stream")
With sm
.Type = 2
.Mode = 3
.Open
.CharSet = "iso-8859-1"
.WriteText s
.Position = 0
.CharSet = cs
DecodeURI = .ReadText(-1)
.Close
End With
Set sm = Nothing
End Function
Response.Write DecodeURI("%B8%A7%CB%B3%C7%E0%CB%C9%D2%A9%D2%B5")
Response.Write DecodeURI("%E6%8A%9A%E9%A1%BA%E9%9D%92%E6%9D%BE%E8%8D%AF%E4%B8%9A")