HttpUtility.HtmlEncode doesn't encode everything

后端 未结 7 948
南方客
南方客 2020-12-16 13:45

I am interacting with a web server using a desktop client program in C# and .Net 3.5. I am using Fiddler to see what traffic the web browser sends, and emulate that. Sadly t

相关标签:
7条回答
  • 2020-12-16 14:21

    It seems horribly inefficient, but the only way I can think to do that is to look through each character:

    public static string MyHtmlEncode(string value)
    {
       // call the normal HtmlEncode first
       char[] chars = HttpUtility.HtmlEncode(value).ToCharArray();
       StringBuilder encodedValue = new StringBuilder();
       foreach(char c in chars)
       {
          if ((int)c > 127) // above normal ASCII
             encodedValue.Append("&#" + (int)c + ";");
          else
             encodedValue.Append(c);
       }
       return encodedValue.ToString();
    }
    
    0 讨论(0)
提交回复
热议问题