通过控制台应用程序进行UrlEncode?
时间:2020-03-05 18:40:24 来源:igfitidea点击:
通常我只会使用:
HttpContext.Current.Server.UrlEncode("url");
但是由于这是一个控制台应用程序,因此HttpContext.Current始终为null。
还有另一种方法可以执行与我相同的操作吗?
解决方案
回答
尝试在HttpUtility类中使用UrlEncode方法。
- http://msdn.microsoft.com/zh-cn/library/system.web.httputility.urlencode.aspx
回答
我不是.NET的人,但是,我们不能使用:
HttpUtility.UrlEncode Method (String)
此处描述:
MSDN上的HttpUtility.UrlEncode方法(字符串)
回答
System.Web中的HttpUtility.UrlEncode(" url")。
回答
使用静态HttpUtility.UrlEncode方法。
回答
我们将要使用
System.Web.HttpUtility.urlencode("url")
确保我们将system.web作为项目中的引用之一。我认为默认情况下,控制台应用程序中不包含它作为参考。
回答
我自己遇到了这个问题,而不是将System.Web程序集添加到我的项目中,而是编写了一个用于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
回答
Kibbee提供了真正的答案。是的,使用HttpUtility.UrlEncode是正确的方法,但是默认情况下,控制台应用程序将无法使用它。我们必须添加对System.Web的引用。要做到这一点,
- 在解决方案资源管理器中,右键单击引用
- 选择"添加参考"
- 在"添加引用"对话框中,使用.NET选项卡
- 向下滚动到System.Web,选择它,然后单击确定
现在,我们可以使用UrlEncode方法。我们仍然要添加,
使用System.Web
在控制台应用程序的顶部,或者在调用方法时使用完整的名称空间,
System.Web.HttpUtility.UrlEncode(someString)
回答
伊恩·霍普金斯(Ian Hopkins)的代码为我完成了窍门,而无需添加对System.Web的引用。这是不使用VB.NET的Cfor的端口:
/// <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)); } }