我写了一些代码来用 C# 发布推文。让我绊倒的一件事是数据的 url 编码,因为似乎有很多选择:
var input = "Hello Ladies + Gentlemen, a signed OAuth request!";
var expected = "Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request%21";
Console.WriteLine(WebUtility.UrlEncode(input) == expected); // False
Console.WriteLine(Uri.EscapeUriString(input) == expected); // False
Console.WriteLine(Uri.EscapeDataString(input) == expected); // True
我现在正试图在 Dart 中做同样的事情。我已经尝试了 Uri 类中的所有编码方法,但似乎没有一个输出相同。
代码:(飞镖垫)
print(Uri.encodeQueryComponent("Hello Ladies + Gentlemen, a signed OAuth request!"));
print(Uri.encodeFull("Hello Ladies + Gentlemen, a signed OAuth request!"));
print(Uri.encodeComponent("Hello Ladies + Gentlemen, a signed OAuth request!"));
输出:
Hello+Ladies+%2B+Gentlemen%2C+a+signed+OAuth+request%21
Hello%20Ladies%20+%20Gentlemen,%20a%20signed%20OAuth%20request!
Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request!
最后一个 ( encodeComponent
) 似乎最接近,只是感叹号是错误的。
是否有按照我的要求进行这种编码的现有方法(与 C# 相同EscapeDataString
)?