tags:

views:

3408

answers:

8

Hi,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.

Is there another method that does the same thing that I could use?

+4  A: 

Try using the UrlEncode method in the HttpUtility class.

  1. http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx
Patrik
+10  A: 

I'm not a .NET guy, but, can't you use:

HttpUtility.UrlEncode Method (String)

Which is described here:

http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx

Andrew

Andrew Taylor
I don't want to rain on everyone's parade, but the mentioned HttpUtility.UrlEncode doesn't seem to be visible even when I include "using System.Web". Does this actually work for someone and if so can you include the actual code?
Kevin Kershaw
You need (must) add System.Web as a reference. Simply putting using System.Web is not enough
Anjisan
I knew about System.Web.HttpContext but it wasn't resolving. Thanks Anjisan for pointing out that I needed to add System.Web as a reference. +1 from me!
Jere.Jones
+1  A: 

HttpUtility.UrlEncode("url") in System.Web.

Vaibhav
+1  A: 

use the static HttpUtility.UrlEncode method.

DannySmurf
+5  A: 

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.

Kibbee
+1  A: 

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
+1  A: 

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 must add a reference to System.Web. To do that,

  1. In your solution explorer, right click on references
  2. Choose "add reference"
  3. In the "Add Reference" dialog box, use the .NET tab
  4. Scroll down to System.Web, select that, and hit ok

NOW you can use the UrlEncode method. You'll still want to add,

using System.Web

at the top of your console app or use the full namespace when calling the method,

System.Web.HttpUtility.UrlEncode(someString)

Anjisan
A: 

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("", 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));
    }
}
David in Dakota