UrlEncode through a console application?

后端 未结 12 1237
我寻月下人不归
我寻月下人不归 2020-12-15 02:16

Normally I would just use:

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

But since this is a console application, HttpContext.Curren

相关标签:
12条回答
  • 2020-12-15 02:49

    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.

    0 讨论(0)
  • 2020-12-15 02:50

    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:

    /// <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));
        }
    }
    
    0 讨论(0)
  • 2020-12-15 02:50

    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.

    ''' <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
    
    0 讨论(0)
  • 2020-12-15 02:58

    Uri.EscapeUriString should not be used for escaping a string to be passed in a URL as it does not encode all characters as you might expect. The '+' is a good example which is not escaped. This then gets converted to a space in the URL since this is what it means in a simple URI. Obviously that causes massive issues the minute you try and pass something like a base 64 encoded string in the URL and spaces appear all over your string at the receiving end.

    You can use HttpUtility.UrlEncode and add the required references to your project (and if you're communicating with a web application then I see no reason why you shouldn't do this).

    Alternatively use Uri.EscapeDataString over Uri.EscapeUriString as explained very well here: https://stackoverflow.com/a/34189188/7391

    0 讨论(0)
  • 2020-12-15 03:00

    Try using the UrlEncode method in the HttpUtility class.

    1. http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx
    0 讨论(0)
  • 2020-12-15 03:05

    Use WebUtility.UrlEncode(string) from System.Net namespace

    0 讨论(0)
提交回复
热议问题