两者有什么区别?
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,它不能说明区别是什么。
发布于 2016-03-09 09:11:01
您可以参考this
不同之处在于空间转义。UrlEncode将它们转义为+符号,UrlPathEncode转义到%20。+和%20只有在它们是每个W3C的QueryString部分的一部分时才是等效的。因此,您不能使用+符号,只使用querystring部分来转义整个URL。
发布于 2016-03-09 09:16:43
不同的是从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声明。
这些方法重载可用于编码整个URL,包括查询-字符串值。
https://stackoverflow.com/questions/35887126
复制相似问题