gpt4 book ai didi

c# - UrlPathEncode 与 UrlEncode

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:33 25 4
gpt4 key购买 nike

有什么区别

HttpUtility.UrlPathEncode(params);

HttpUtility.UrlEncode(params);

我查看了 MSDN 页面
https://msdn.microsoft.com/en-us/library/system.web.httputility.urlpathencode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx

但它只告诉你不要使用 UrlPathEncode,并没有告诉你有什么区别。

最佳答案

不同之处在于一个编码字符串的 Url 和一个编码来自 Url 的路径部分(这意味着查询字符串之前的 url 部分),这是它的实现方式:

 /// <summary>Encodes a URL string.</summary>
/// <returns>An encoded string.</returns>
/// <param name="str">The text to encode. </param>
public static string UrlEncode(string str)
{
if (str == null)
{
return null;
}
return HttpUtility.UrlEncode(str, Encoding.UTF8);
}

这里是 UrlPathEncode 的实现:

/// <summary>Encodes the path portion of a URL string for reliable HTTP transmission from the Web server to a client.</summary>
/// <returns>The URL-encoded text.</returns>
/// <param name="str">The text to URL-encode. </param>
public static string UrlPathEncode(string str)
{
if (str == null)
{
return null;
}
int num = str.IndexOf('?'); // <--- notice this
if (num >= 0)
{
return HttpUtility.UrlPathEncode(str.Substring(0, num)) + str.Substring(num);
}
return HttpUtility.UrlEncodeSpaces(HttpUtility.UrlEncodeNonAscii(str, Encoding.UTF8));
}

并且 msdn 还声明了 HttpUtility.UrlEnocde:

These method overloads can be used to encode the entire URL, including query-string values.

关于c# - UrlPathEncode 与 UrlEncode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com