之前一直用自己写的URLEncode方法,但对于UTF-8的字符处理不太好,勉强能用,最近测试饭盒的搜索功能时发现勉强能用就等于不能用(对汉字的编码完全不对),只好重新把HTTPUtility.URLEncode引入进来,然后对其不正确的处理进行修正。
以下为相应代码,有两点要注意:
1. 需要先行添加System.Web的引用,如果找不到这个引用,检查目标Framework是不是用了Client Profile。
2. 最后一次将%253D替换为%3D用于将二次编码的「=」符号(编码后为%3D,二次编码后为%253D)替换回一次编码。理论上按照RFC 5849和饭否自己的文档,GET方法的QueryString应当经过一次URL Encode,然后在生成Base String然后签名的时候被二次编码,但饭否的API如果在Base String中检测到%253D时,将会返回Invalid Signature错误,并且给出认可的Base String中「=」显示为%3D,不知道是不是部署时出现的问题。
Private Function doURLEncode(ByVal s As String) As String
'URLEncode function with corrections for OAuth Basestr making
s = HttpUtility.UrlEncode(s).Replace("+", "%20")
'Make the lowercased percent-encoded codes into uppercase
s = RegularExpressions.Regex.Replace(s, "(%[0-9a-f][0-9a-f])", Function(c) c.Value.ToUpper)
'Encode the chars RFC requested to encode but the HTTPUtility.URLEncode didn't
s = s.Replace("(", "%28").Replace(")", "%29").Replace("$", "%24").Replace("!", "%21").Replace("*", "%2A").Replace("'", "%27")
'Decode the char [~] RFC requested NOT to encode but HTTPUtility.URLEncode did
s = s.Replace("%7E", "~")
'Both the RFC and API document did not say [=] is a special character, but the API won't accept %253D.
s = s.Replace("%253D", "%3D")
Return s
End Function
注:所有评论将在审核通过后显示,请不要在评论内容的任何位置出现链接,否则您的评论将被自动移入回收站,且永远不会被复审。
All comments will be available after being manually reviewed, please do not include any links anywhere in your comment, otherwise your comment will be automatically deleted and are not eligible for review.