<p>Normally I would just use:</p>

<pre><code>HttpContext.Current.Server.UrlEncode("url");
</code></pre>

<p>But since this is a console application, <code>HttpContext.Current</code> is always going to be <code>null</code>.</p>

<p>Is there another method that does the same thing that I could use?</p>

[{'answer_id': 14734, 'author': 'Patrik Svensson', 'author_id': 936, 'author_profile': 'https://Stackoverflow.com/users/936', 'pm_score': 2, 'selected': False, 'text': '<p>Try using the UrlEncode method in the HttpUtility class.</p>\n\n<ol>\n<li><a href="http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx</a></li>\n</ol>\n'}, {'answer_id': 14736, 'author': 'Andrew Taylor', 'author_id': 1776, 'author_profile': 'https://Stackoverflow.com/users/1776', 'pm_score': 6, 'selected': False, 'text': '<p>I\'m not a .NET guy, but, can\'t you use:</p>\n\n<pre><code>HttpUtility.UrlEncode Method (String)\n</code></pre>\n\n<p>Which is described here:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx" rel="noreferrer">HttpUtility.UrlEncode Method (String) on MSDN</a></p>\n'}, {'answer_id': 14739, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 1, 'selected': False, 'text': '<p>HttpUtility.UrlEncode("url") in System.Web.</p>\n'}, {'answer_id': 14742, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 1, 'selected': False, 'text': '<p>use the static HttpUtility.UrlEncode method. </p>\n'}, {'answer_id': 14745, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 3, 'selected': False, 'text': '<p>You\'ll want to use </p>\n\n<pre><code>System.Web.HttpUtility.urlencode("url")\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 390650, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>\'\'\' &lt;summary&gt;\n\'\'\' URL encoding class.  Note: use at your own risk.\n\'\'\' Written by: Ian Hopkins (http://www.lucidhelix.com)\n\'\'\' Date: 2008-Dec-23\n\'\'\' &lt;/summary&gt;\nPublic Class UrlHelper\n    Public Shared Function Encode(ByVal str As String) As String\n        Dim charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*\'()"))\n        Dim pattern = String.Format("[^{0}]", charClass)\n        Dim evaluator As New MatchEvaluator(AddressOf EncodeEvaluator)\n\n        \' replace the encoded characters\n        Return Regex.Replace(str, pattern, evaluator)\n    End Function\n\n    Private Shared Function EncodeEvaluator(ByVal match As Match) As String\n    \' Replace the " "s with "+"s\n        If (match.Value = " ") Then\n            Return "+"\n        End If\n        Return String.Format("%{0:X2}", Convert.ToInt32(match.Value.Chars(0)))\n    End Function\n\n    Public Shared Function Decode(ByVal str As String) As String\n        Dim evaluator As New MatchEvaluator(AddressOf DecodeEvaluator)\n\n        \' Replace the "+"s with " "s\n        str = str.Replace("+"c, " "c)\n\n        \' Replace the encoded characters\n        Return Regex.Replace(str, "%[0-9a-zA-Z][0-9a-zA-Z]", evaluator)\n    End Function\n\n    Private Shared Function DecodeEvaluator(ByVal match As Match) As String\n        Return "" + Convert.ToChar(Integer.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber))\n    End Function\nEnd Class\n</code></pre>\n'}, {'answer_id': 650823, 'author': 'Anjisan', 'author_id': 25304, 'author_profile': 'https://Stackoverflow.com/users/25304', 'pm_score': 2, 'selected': False, 'text': '<p>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 <strong>must</strong> add a reference to System.Web.  To do that,</p>\n\n<ol>\n<li>In your solution explorer, right click on references</li>\n<li>Choose "add reference"</li>\n<li>In the "Add Reference" dialog box, use the .NET tab</li>\n<li>Scroll down to System.Web, select that, and hit ok</li>\n</ol>\n\n<p>NOW you can use the UrlEncode method.  You\'ll still want to add,</p>\n\n<p>using System.Web </p>\n\n<p>at the top of your console app or use the full namespace when calling the method,</p>\n\n<p>System.Web.HttpUtility.UrlEncode(someString)</p>\n'}, {'answer_id': 4006817, 'author': 't3rse', 'author_id': 64, 'author_profile': 'https://Stackoverflow.com/users/64', 'pm_score': 4, 'selected': False, 'text': '<p>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: </p>\n\n<pre><code>/// &lt;summary&gt;\n/// URL encoding class.  Note: use at your own risk.\n/// Written by: Ian Hopkins (http://www.lucidhelix.com)\n/// Date: 2008-Dec-23\n/// (Ported to C# by t3rse (http://www.t3rse.com))\n/// &lt;/summary&gt;\npublic class UrlHelper\n{\n    public static string Encode(string str) {\n        var charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*\'()"));\n        return Regex.Replace(str, \n            String.Format("[^{0}]", charClass),\n            new MatchEvaluator(EncodeEvaluator));\n    }\n\n    public static string EncodeEvaluator(Match match)\n    {\n        return (match.Value == " ")?"+" : String.Format("%{0:X2}", Convert.ToInt32(match.Value[0]));\n    }\n\n    public static string DecodeEvaluator(Match match) {\n        return Convert.ToChar(int.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber)).ToString();\n    }\n\n    public static string Decode(string str) \n    {\n        return Regex.Replace(str.Replace(\'+\', \' \'), "%[0-9a-zA-Z][0-9a-zA-Z]", new MatchEvaluator(DecodeEvaluator));\n    }\n}\n</code></pre>\n'}, {'answer_id': 8931490, 'author': 'Ostati', 'author_id': 2654100, 'author_profile': 'https://Stackoverflow.com/users/2654100', 'pm_score': 7, 'selected': True, 'text': '<p>Try this!</p>\n\n<pre><code>Uri.EscapeUriString(url);\n</code></pre>\n\n<p>Or</p>\n\n<pre><code>Uri.EscapeDataString(data)\n</code></pre>\n\n<p>No need to reference System.Web.</p>\n\n<p><strong>Edit:</strong> Please see <a href="https://stackoverflow.com/a/34189188/7391">another</a> SO answer for more...</p>\n'}, {'answer_id': 35093168, 'author': 'Devson Technologies', 'author_id': 1232988, 'author_profile': 'https://Stackoverflow.com/users/1232988', 'pm_score': 3, 'selected': False, 'text': '<p>Add <code>using System.Net;</code> then use: <code>WebUtility.UrlDecode(string)</code><br />\nor Fully-Qualify: <code>System.Net.WebUtility.UrlDecode(string)</code></p>\n<p>No need to add any additional References.<br />\nThe <code>WebUtility</code> is included (by Default) in <code>System</code> (under the &quot;<em>References</em>&quot; Project Folder).</p>\n'}, {'answer_id': 38713004, 'author': 'Girjesh Kumar Vishwakarma', 'author_id': 5126961, 'author_profile': 'https://Stackoverflow.com/users/5126961', 'pm_score': 0, 'selected': False, 'text': '<p>Best thing is to Add Reference to System.web..dll</p>\n\n<p>and use \nvar EncodedUrl=System.Web.HttpUtility.UrlEncode("URL_TEXT");</p>\n\n<p>You can find the File at <a href="http://originaldll.com/download/11019.dll" rel="nofollow noreferrer">System.web.dll</a></p>\n'}, {'answer_id': 48805794, 'author': 'Jason', 'author_id': 7391, 'author_profile': 'https://Stackoverflow.com/users/7391', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>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).</p>\n\n<p>Alternatively use Uri.EscapeDataString over Uri.EscapeUriString as explained very well here: <a href="https://stackoverflow.com/a/34189188/7391">https://stackoverflow.com/a/34189188/7391</a></p>\n'}]
['https://Stackoverflow.com/questions/14731', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1469/']