HttpUtility.HtmlEncode
和HttpUtility.UrlEncode`编码了哪些字符?文档似乎没有提到编码的确切字符。
答案 0 :(得分:1)
以下单元测试提供了部分答案。然而我真正想要的是UrlEncode() - 参见上一个测试。希望这有助于某人
public class HtmlDecodeTest
{
////https://perishablepress.com/stop-using-unsafe-characters-in-urls/
//char[] chars = { ';', '/', '?', ':', '@', '=', '&', '<', '>', '#', '%', '{', '}', '|', '\\', '^', '~', '[', ']', '`', ' ' };
[Fact]
public void NotEncodedTest()
{
var msg = ";,/,?,:,@,=,#,%,{,},|.\\,^,~,[,],` ";
var encoded = HttpUtility.HtmlEncode(msg);
Assert.Equal(msg, encoded);
var decoded = HttpUtility.HtmlDecode(encoded);
Assert.Equal(msg, decoded);
}
[Fact]
public void EncodedTest()
{
var msg = "<,>,&";
var encoded = HttpUtility.HtmlEncode(msg);
Assert.Equal("<,>,&", encoded);
var decoded = HttpUtility.HtmlDecode(encoded);
Assert.Equal(msg, decoded);
}
[Fact]
public void UrlEncodedTest()
{
var msg = "<>&/?:@=#%{}|\\^~[]` end";
var encoded = HttpUtility.UrlEncode(msg);
char[] invalidChars = { ';', '/', '?', ':', '@', '=', '&', '<', '>', '#', '{',
'}', '|', '\\', '^', '~', '[', ']', '`', ' ' };
Assert.True(encoded.IndexOfAny(invalidChars) == -1);
Assert.Equal("%3c%3e%26%2f%3f%3a%40%3d%23%25%7b%7d%7c%5c%5e%7e%5b%5d%60+end", encoded);
var decoded = HttpUtility.UrlDecode(encoded);
Assert.Equal(msg, decoded);
}
}