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?
The HttpUtility. UrlEncode method uses UTF-8 encoding by default. Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter. UrlEncode is a convenient way to access the UrlEncode method at run time from an ASP.NET application.
UrlEncode is a convenient way to access the HttpUtility. UrlEncode method at run time from an ASP.NET application. Internally, UrlEncode uses HttpUtility. UrlEncode to encode strings. To encode or decode values outside of a web application, use the WebUtility class.
General Answer. The general answer to your question is that it depends. And you get to decide by specifying what your "Content-Type" is in the HTTP headers. A value of "application/x-www-form-urlencoded" means that your POST body will need to be URL encoded just like a GET parameter string.
The URLEncode method applies URL encoding rules, including escape characters, to a specified string. URLEncode converts characters as follows: Spaces ( ) are converted to plus signs (+). Non-alphanumeric characters are escaped to their hexadecimal representation.
I'm not a .NET guy, but, can't you use:
HttpUtility.UrlEncode Method (String)
Which is described here:
HttpUtility.UrlEncode Method (String) on MSDN
Try this!
Uri.EscapeUriString(url);
Or
Uri.EscapeDataString(data)
No need to reference System.Web.
Edit: Please see another SO answer for more...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With