通常情况下,我将只是使用:

HttpContext.Current.Server.UrlEncode("url");

但是,由于这是一个控制台应用程序, HttpContext.Current 总是要 null.

是否有另一个方法,该方法做同样的事情,我可以使用?

有帮助吗?

解决方案

试试这个!

Uri.EscapeUriString(url);

Uri.EscapeDataString(data)

没有必要参考系统。网。

编辑: 请看看 另一个 所以回答的更多...

其他提示

我不是一个。净的家伙,但是,你不能使用:

HttpUtility.UrlEncode Method (String)

这是描述:

HttpUtility.UrlEncode方法(String)在MSDN

代码从伊恩*霍普金斯做的把戏对我没有必要添加一个参考系统。网。这里是一个口C#对于那些不使用VB.NET:

/// <summary>
/// URL encoding class.  Note: use at your own risk.
/// Written by: Ian Hopkins (http://www.lucidhelix.com)
/// Date: 2008-Dec-23
/// (Ported to C# by t3rse (http://www.t3rse.com))
/// </summary>
public class UrlHelper
{
    public static string Encode(string str) {
        var charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()"));
        return Regex.Replace(str, 
            String.Format("[^{0}]", charClass),
            new MatchEvaluator(EncodeEvaluator));
    }

    public static string EncodeEvaluator(Match match)
    {
        return (match.Value == " ")?"+" : String.Format("%{0:X2}", Convert.ToInt32(match.Value[0]));
    }

    public static string DecodeEvaluator(Match match) {
        return Convert.ToChar(int.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber)).ToString();
    }

    public static string Decode(string str) 
    {
        return Regex.Replace(str.Replace('+', ' '), "%[0-9a-zA-Z][0-9a-zA-Z]", new MatchEvaluator(DecodeEvaluator));
    }
}

你会想要使用

System.Web.HttpUtility.urlencode("url")

确保你已经系统。网作为一个参考文献在您的项目。我不认为这是包括作为参考默认情况下,在控制台应用程序。

我遇到这个问题我自己,而不是加入该系统。网组件到我的项目,我写了一个类编码的网址(其很简单,和我已经做了一些测试,但是不会很多)。我已经包括源代码如下。请:留在上面如果你再用这个,不要责怪我,如果它打破,从中学习的代码。

''' <summary>
''' URL encoding class.  Note: use at your own risk.
''' Written by: Ian Hopkins (http://www.lucidhelix.com)
''' Date: 2008-Dec-23
''' </summary>
Public Class UrlHelper
    Public Shared Function Encode(ByVal str As String) As String
        Dim charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()"))
        Dim pattern = String.Format("[^{0}]", charClass)
        Dim evaluator As New MatchEvaluator(AddressOf EncodeEvaluator)

        ' replace the encoded characters
        Return Regex.Replace(str, pattern, evaluator)
    End Function

    Private Shared Function EncodeEvaluator(ByVal match As Match) As String
    ' Replace the " "s with "+"s
        If (match.Value = " ") Then
            Return "+"
        End If
        Return String.Format("%{0:X2}", Convert.ToInt32(match.Value.Chars(0)))
    End Function

    Public Shared Function Decode(ByVal str As String) As String
        Dim evaluator As New MatchEvaluator(AddressOf DecodeEvaluator)

        ' Replace the "+"s with " "s
        str = str.Replace("+"c, " "c)

        ' Replace the encoded characters
        Return Regex.Replace(str, "%[0-9a-zA-Z][0-9a-zA-Z]", evaluator)
    End Function

    Private Shared Function DecodeEvaluator(ByVal match As Match) As String
        Return "" + Convert.ToChar(Integer.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber))
    End Function
End Class

使用 WebUtility.UrlEncode(string)System.Net namespace

Kibbee提供真正的答案。是的,HttpUtility.UrlEncode是正确的方法来使用,但它将不能通过默认为一种控制台应用程序。你的 必须 增加一个参考系统。网。做到这一点,

  1. 在你的解决方案资源管理器,右击引用
  2. 选择"添加参照"
  3. 在"参加"对话框使用。净卡
  4. 滚下来的系统。网页,选择,并打好

现在你可以使用UrlEncode方法。你仍然想加,

使用的系统。网

在你的控制台应用程序,或者使用全名称空间时调用的方法,

系统。网。HttpUtility.UrlEncode(someString)

HttpUtility.UrlEncode("url")中的系统。网。

使用静态HttpUtility.UrlEncode方法。

最好的办法是添加参照System.web..dll

和使用 var EncodedUrl=系统。网。HttpUtility.UrlEncode("URL_TEXT");

你可以找到的文件 System.web.dll

Uri。EscapeUriString不应该被用于逃避的一个字符串通过在一个网址,它没有编码的所有人物,你可能期望。该'+'是一个很好的例子,这不是逃跑了。这个,然后被转换到空间在URL由于这是什么意思在一个简单的URI。显然,导致大规模的问题分钟,你尝试并通过东西就像一个基64编码的字符串中URL和空间出现在你串在接收端。

你可以使用HttpUtility.UrlEncode和添加所需要的引用你的项目(如果你的沟通与网络应用程序,然后我看不出有任何理由为什么你不应该这样做).

或者使用Uri。EscapeDataString过Uri。EscapeUriString作为解释的非常好,这里: https://stackoverflow.com/a/34189188/7391

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top