我正在尝试根据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
Run Code Online (Sandbox Code Playgroud)
我的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();
}
Run Code Online (Sandbox Code Playgroud)
这些测试失败了:
[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"
------------^
Run Code Online (Sandbox Code Playgroud)
这些测试失败了:
[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"
------------^
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我确实通过了以下测试:
[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");
}
Run Code Online (Sandbox Code Playgroud)
想要在这里做到这一点的任何人的编辑是工作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();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是你没有考虑到这一部分:
- 如果文本值尚未按[RFC3629]编码,则首先将其编码为UTF-8八位字节.这不包括不供人类消费的二进制值.
所以你应该实际使用:
byte[] bytes = Encoding.UTF8.GetBytes(symbol.ToString());
foreach (byte b in bytes)
{
input.AppendFormat("%{0:x2}", b);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
483 次 |
| 最近记录: |