Day 8644 VB.Net URLEncode for OAuth

之前一直用自己写的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

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.
您的评论将在通过审核后显示。

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.
请不要在评论中插入任何链接,否则将被自动归类为垃圾评论,且永远不会被提交给博主进行复审。

*