0

我正在尝试根据 RFC 5849 OAuth 1.0 将 å 编码为 %C3%A5

http://tools.ietf.org/rfc/rfc5849.txt

这可以在 GoCardless Ruby 规范 https://github.com/gocardless/gocardless-ruby/blob/master/spec/utils_spec.rb中看到

 it "encodes non-ascii alpha characters" do
    subject["å"].should == "%C3%A5"
 end

我的 C# 代码如下所示:

    private const string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

    public static string PercentEncode(string value)
    {
        var input = new StringBuilder();
        foreach (char symbol in value)
        {
            if (UnreservedChars.IndexOf(symbol) != -1)
            {
                input.Append(symbol);
            }
            else
            {
                input.Append('%' + String.Format("{0:X2}", (int)symbol));
            }
        }

        return input.ToString();
    }

这些测试失败:

[Test]
public void It_encodes_non_ascii_alpha_characters()
{
    Util.PercentEncode("å").ShouldBe("%C3%A5"); 
}

Expected string length 6 but was 3. Strings differ at index 1.
  Expected: "%C3%A5"
  But was:  "%E5"
  ------------^

这些测试失败:

[Test]
public void It_encodes_other_non_ascii_characters()
{
    Util.PercentEncode("支払い").ShouldBe("%E6%94%AF%E6%89%95%E3%81%84");
}

Expected string length 27 but was 15. Strings differ at index 1.
 Expected: "%E6%94%AF%E6%89%95%E3%81%84"
 But was:  "%652F%6255%3044"
 ------------^

顺便说一句,我确实通过了这些测试:

[Test]
public void It_encodes_reserved_ascii_characters()
{
    Util.PercentEncode(" !\"#$%&'()").ShouldBe("%20%21%22%23%24%25%26%27%28%29");
    Util.PercentEncode("*+,/{|}:;").ShouldBe("%2A%2B%2C%2F%7B%7C%7D%3A%3B");
    Util.PercentEncode("<=>?@[\\]^`").ShouldBe("%3C%3D%3E%3F%40%5B%5C%5D%5E%60");
}

任何想要在这里执行此操作的人的编辑是有效的 C# 代码:

public class Util
{
    private const string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

    public static string PercentEncode(string value)
    {
        var input = new StringBuilder();
        foreach (char symbol in value)
        {
            if (UnreservedChars.IndexOf(symbol) != -1)
            {
                input.Append(symbol);
            }
            else
            {
                byte[] bytes = Encoding.UTF8.GetBytes(symbol.ToString());
                foreach (byte b in bytes)
                {
                    input.AppendFormat("%{0:X2}", b);
                }
            }
        }

        return input.ToString();
    }
}
4

1 回答 1

5

问题是你没有考虑到这部分:

  1. 文本值首先根据 [RFC3629] 编码为 UTF-8 八位字节(如果它们尚未编码)。这不包括不适合人类消费的二进制值。

所以你应该实际使用:

byte[] bytes = Encoding.UTF8.GetBytes(symbol.ToString());
foreach (byte b in bytes)
{
    input.AppendFormat("%{0:x2}", b);
}
于 2012-04-04T18:41:25.300 回答