代码之家  ›  专栏  ›  技术社区  ›  Martin Brown

如何不使用URL编码系统。网络?

  •  295
  • Martin Brown  · 技术社区  · 14 年前

    我正在尝试编写一个windows客户端应用程序,它调用一个web站点来获取数据。为了保持安装到最低限度,我只尝试使用 dlls in the .NET Framework Client Profile

    8 回复  |  直到 13 年前
        1
  •  320
  •   T.Todua Laurent W.    7 年前

    System.Uri.EscapeUriString() 某些字符可能会有问题,对我来说,这是字符串中的数字/磅'#'符号。

    System.Uri.EscapeDataString() //Works excellent with individual values
    

    下面是一个问题的答案,解释了两者的区别:

    What's the difference between EscapeUriString and EscapeDataString?

    Uri.EscapeDataString() 在任何方面。

        2
  •  254
  •   T.Todua Laurent W.    6 年前

    WebUtility

    只是为了格式化我提交这个作为一个答案。

    找不到比较它们的好例子,所以:

    string testString = "http://test# space 123/text?var=val&another=two";
    Console.WriteLine("UrlEncode:         " + System.Web.HttpUtility.UrlEncode(testString));
    Console.WriteLine("EscapeUriString:   " + Uri.EscapeUriString(testString));
    Console.WriteLine("EscapeDataString:  " + Uri.EscapeDataString(testString));
    Console.WriteLine("EscapeDataReplace: " + Uri.EscapeDataString(testString).Replace("%20", "+"));
    
    Console.WriteLine("HtmlEncode:        " + System.Web.HttpUtility.HtmlEncode(testString));
    Console.WriteLine("UrlPathEncode:     " + System.Web.HttpUtility.UrlPathEncode(testString));
    
    //.Net 4.0+
    Console.WriteLine("WebUtility.HtmlEncode: " + WebUtility.HtmlEncode(testString));
    //.Net 4.5+
    Console.WriteLine("WebUtility.UrlEncode:  " + WebUtility.UrlEncode(testString));
    

    UrlEncode:             http%3a%2f%2ftest%23+space+123%2ftext%3fvar%3dval%26another%3dtwo
    EscapeUriString:       http://test#%20space%20123/text?var=val&another=two
    EscapeDataString:      http%3A%2F%2Ftest%23%20space%20123%2Ftext%3Fvar%3Dval%26another%3Dtwo
    EscapeDataReplace:     http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo
    
    HtmlEncode:            http://test# space 123/text?var=val&another=two
    UrlPathEncode:         http://test#%20space%20123/text?var=val&another=two
    
    //.Net 4.0+
    WebUtility.HtmlEncode: http://test# space 123/text?var=val&another=two
    //.Net 4.5+
    WebUtility.UrlEncode:  http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo
    

    在.Net 4.5+中使用 Web实用程序 .UrlEncode

    HttpUtility.UrlEncode
    Uri.EscapeDataString(testString).Replace("%20", "+").Replace("'", "%27").Replace("~", "%7E")
    注: EscapeUriString 将保留一个有效的uri字符串,这将使它使用尽可能多的纯文本字符。


    https://stackoverflow.com/a/11236038/555798

    换行符 所有这些都列在这里(除了 HttpUtility.HtmlEncode )将转换 "\n\r" 进入之内 %0a%0d %0A%0D

        3
  •  54
  •   Matthew Manela    14 年前
        4
  •  22
  •   Elmue    11 年前

    Uri.EscapeUriString 具有 Uri.EscapeDataString 对于0到255之间的所有字符。

    结果如下:

    ******* Different *******
    
    '#' -> Uri "#" Data "%23"
    '$' -> Uri "$" Data "%24"
    '&' -> Uri "&" Data "%26"
    '+' -> Uri "+" Data "%2B"
    ',' -> Uri "," Data "%2C"
    '/' -> Uri "/" Data "%2F"
    ':' -> Uri ":" Data "%3A"
    ';' -> Uri ";" Data "%3B"
    '=' -> Uri "=" Data "%3D"
    '?' -> Uri "?" Data "%3F"
    '@' -> Uri "@" Data "%40"
    
    
    ******* Not escaped *******
    
    '!' -> Uri "!" Data "!"
    ''' -> Uri "'" Data "'"
    '(' -> Uri "(" Data "("
    ')' -> Uri ")" Data ")"
    '*' -> Uri "*" Data "*"
    '-' -> Uri "-" Data "-"
    '.' -> Uri "." Data "."
    '_' -> Uri "_" Data "_"
    '~' -> Uri "~" Data "~"
    
    '0' -> Uri "0" Data "0"
    .....
    '9' -> Uri "9" Data "9"
    
    'A' -> Uri "A" Data "A"
    ......
    'Z' -> Uri "Z" Data "Z"
    
    'a' -> Uri "a" Data "a"
    .....
    'z' -> Uri "z" Data "z"
    
    ******* UTF 8 *******
    
    .....
    'Ò' -> Uri "%C3%92" Data "%C3%92"
    'Ó' -> Uri "%C3%93" Data "%C3%93"
    'Ô' -> Uri "%C3%94" Data "%C3%94"
    'Õ' -> Uri "%C3%95" Data "%C3%95"
    'Ö' -> Uri "%C3%96" Data "%C3%96"
    .....
    

    EscapeUriString 用于对URL进行编码,而 EscapeDataString '=' ';' .

        5
  •  16
  •   Sprague    14 年前
        6
  •  8
  •   Darin Dimitrov    14 年前

    下面是一个发送POST请求的示例,该请求使用 application/x-www-form-urlencoded

    using (var client = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "param1", "value1" },
            { "param2", "value2" },
        };
        var result = client.UploadValues("http://foo.com", values);
    }
    
        7
  •  0
  •   Aaron Qiu    8 年前

    不使用URL编码系统.Web:

    String s = System.Net.WebUtility.UrlEncode(str);
    //fix some different between WebUtility.UrlEncode and HttpUtility.UrlEncode
    s = Regex.Replace(s, "(%[0-9A-F]{2})", c => c.Value.ToLowerInvariant());
    

    更多详细信息: https://www.samnoble.co.uk/2014/05/21/beware-webutility-urlencode-vs-httputility-urlencode/

        8
  •  -3
  •   Ondrej Janacek    11 年前
    System.Net.WebUtility.HtmlDecode