Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UrlPathEncode与UrlEncode_C#_.net_Urlencode - Fatal编程技术网

C# UrlPathEncode与UrlEncode

C# UrlPathEncode与UrlEncode,c#,.net,urlencode,C#,.net,Urlencode,两者的区别是什么 HttpUtility.UrlPathEncode(params); 及 我查看了MSDN页面 但它只告诉您不要使用UrlPathEncode,它没有告诉您有什么区别 您可以参考 不同之处在于空间逃逸。UrlEncode将其转义为+符号,UrlPathEncode转义为%20+根据W3C,只有当它们是查询字符串部分的一部分时,%20才是等效的。所以,您不能使用+符号来转义整个URL,而只能转义查询字符串部分 不同之处在于,一个编码字符串的Url,另一个编码Url的路径部分

两者的区别是什么

HttpUtility.UrlPathEncode(params);

我查看了MSDN页面

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

您可以参考

不同之处在于空间逃逸。UrlEncode将其转义为+符号,UrlPathEncode转义为%20+根据W3C,只有当它们是查询字符串部分的一部分时,%20才是等效的。所以,您不能使用+符号来转义整个URL,而只能转义查询字符串部分


不同之处在于,一个编码字符串的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);
 }
///对URL字符串进行编码。
///经过编码的字符串。
///要编码的文本。
公共静态字符串UrlEncode(字符串str)
{
如果(str==null)
{
返回null;
}
返回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));
}
///对URL字符串的路径部分进行编码,以实现从Web服务器到客户端的可靠HTTP传输。
///URL编码的文本。
///要进行URL编码的文本。
公共静态字符串UrlPathEncode(字符串str)
{
如果(str==null)
{
返回null;
}
int num=str.IndexOf('?');/=0)
{
返回HttpUtility.UrlPathEncode(str.Substring(0,num))+str.Substring(num);
}
返回HttpUtility.UrlEncodeSpaces(HttpUtility.urlencodenascii(str,Encoding.UTF8));
}
msdn还声明了
HttpUtility.UrlEnocde

这些方法重载可用于编码整个URL,包括查询字符串值


此答案的可能副本不应被接受。这两种方法非常不同。UrlEncode()对字符串中的所有内容进行编码。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));
}