【问题标题】:System.Net.WebUtility.UrlEncode and System.Web.HttpUtility.UrlEncode methods differencesSystem.Net.WebUtility.UrlEncode 和 System.Web.HttpUtility.UrlEncode 方法的区别
【发布时间】:2019-06-25 07:31:54
【问题描述】:

根据System.Net.WebUtility.UrlEncode(String)方法的documentation,字符代码应该用小写字母编码,至少在示例中是这样说明的:“ 例如,当嵌入要在 URL 中传输的文本块中时,字符 被编码为 %3c 和 %3e。" 但我把它们都写成大写。例如这段代码:

string url = "https://host<test>";
Console.WriteLine("system.net: {0}", System.Net.WebUtility.UrlEncode(url);

给我以下内容:

system.net:https%3A%2F%2Fhost%3Ctest%3E

另一方面,System.Web.HttpUtility.UrlEncode 给了我所有的小写:

string url = "https://host<test>";
Console.WriteLine("system.web: {0}", System.Web.HttpUtility.UrlEncode(url);

输出:

system.web:https%3a%2f%2fhost%3ctest%3e

这是预期的吗?

【问题讨论】:

    标签: c# url


    【解决方案1】:

    这是预期的吗?

    看起来像预期的那样。浏览源代码WebUtility 我看到公共UrlEncode 调用其中一个私有的

        public static string UrlEncode(string value)
        {
          // code stripped
            return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, false /* alwaysCreateNewReturnValue */));
        }
    
        //private one makes a call to IntToHex
        private static byte[] UrlEncode(byte[] bytes, int offset, int count)
        {
          expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);
    
        // IntToHex casting to uppercase character, reason being every 
        // encoded character is returning as uppercase.
        private static char IntToHex(int n)
        {
           //code stripped
            else
                return (char)(n - 10 + (int)'A');  //here
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 2016-01-31
      • 2013-06-12
      • 1970-01-01
      • 2017-12-02
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多