C# UrlEncode 通过控制台应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UrlEncode through a console application?
提问by KevinUK
Normally I would just use:
通常我只会使用:
HttpContext.Current.Server.UrlEncode("url");
But since this is a console application, HttpContext.Current
is always going to be null
.
但由于这是一个控制台应用程序,HttpContext.Current
所以总是会是null
.
Is there another method that does the same thing that I could use?
有没有另一种方法可以做同样的事情,我可以使用?
采纳答案by Ostati
回答by Patrik Svensson
Try using the UrlEncode method in the HttpUtility class.
尝试使用 HttpUtility 类中的 UrlEncode 方法。
回答by Andrew Taylor
I'm not a .NET guy, but, can't you use:
我不是 .NET 人,但是,你不能使用:
HttpUtility.UrlEncode Method (String)
Which is described here:
此处描述:
回答by Vaibhav
HttpUtility.UrlEncode("url") in System.Web.
System.Web 中的 HttpUtility.UrlEncode("url")。
回答by TheSmurf
use the static HttpUtility.UrlEncode method.
使用静态 HttpUtility.UrlEncode 方法。
回答by Kibbee
You'll want to use
你会想要使用
System.Web.HttpUtility.urlencode("url")
Make sure you have system.web as one of the references in your project. I don't think it's included as a reference by default in console applications.
确保将 system.web 作为项目中的引用之一。我不认为它默认包含在控制台应用程序中作为参考。
回答by Kibbee
I ran into this problem myself, and rather than add the System.Web assembly to my project, I wrote a class for encoding/decoding URLs (its pretty simple, and I've done some testing, but not a lot). I've included the source code below. Please: leave the comment at the top if you reuse this, don't blame me if it breaks, learn from the code.
我自己遇到了这个问题,我没有将 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
回答by Anjisan
Kibbee offers the real answer. Yes, HttpUtility.UrlEncode is the right method to use, but it will not be available by default for a console application. You mustadd a reference to System.Web. To do that,
Kibbee 提供了真正的答案。是的,HttpUtility.UrlEncode 是正确的使用方法,但默认情况下它不适用于控制台应用程序。您必须添加对 System.Web 的引用。要做到这一点,
- In your solution explorer, right click on references
- Choose "add reference"
- In the "Add Reference" dialog box, use the .NET tab
- Scroll down to System.Web, select that, and hit ok
- 在您的解决方案资源管理器中,右键单击引用
- 选择“添加参考”
- 在“添加引用”对话框中,使用 .NET 选项卡
- 向下滚动到 System.Web,选择它,然后点击确定
NOW you can use the UrlEncode method. You'll still want to add,
现在您可以使用 UrlEncode 方法。你还想补充,
using System.Web
使用 System.Web
at the top of your console app or use the full namespace when calling the method,
在控制台应用程序的顶部或在调用方法时使用完整的命名空间,
System.Web.HttpUtility.UrlEncode(someString)
System.Web.HttpUtility.UrlEncode(someString)
回答by t3rse
The code from Ian Hopkins does the trick for me without having to add a reference to System.Web. Here is a port to C# for those who are not using VB.NET:
Ian Hopkins 的代码为我解决了这个问题,而无需添加对 System.Web 的引用。对于那些不使用 VB.NET 的人,这是一个 C# 的端口:
/// <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));
}
}
回答by Devson Technologies
Use WebUtility.UrlEncode(string)
from System.Net
namespace
WebUtility.UrlEncode(string)
从System.Net
命名空间使用