Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you UrlEncode without using System.Web?

I am trying to write a windows client application that calls a web site for data. To keep the install to a minimum I am trying only use dlls in the .NET Framework Client Profile. Trouble is that I need to UrlEncode some parameters, is there an easy way to do this without importing System.Web.dll which is not part of the Client Pofile?

like image 684
Martin Brown Avatar asked Oct 01 '10 15:10

Martin Brown


People also ask

What is the difference between Htmlencode and UrlEncode?

HTMLEncoding turns this character into "<" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap. Save this answer. Show activity on this post.

Do I need to UrlEncode?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

What does WebUtility UrlEncode do?

UrlDecode(String) Method (System.Net) Converts a string that has been encoded for transmission in a URL into a decoded string.

How do you escape space in URL?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.


2 Answers

System.Uri.EscapeUriString() can be problematic with certain characters, for me it was a number / pound '#' sign in the string.

If that is an issue for you, try:

System.Uri.EscapeDataString() //Works excellent with individual values 

Here is a SO question answer that explains the difference:

What's the difference between EscapeUriString and EscapeDataString?

and recommends to use Uri.EscapeDataString() in any aspect.

like image 96
ToddBFisher Avatar answered Sep 27 '22 19:09

ToddBFisher


In .Net 4.5+ use WebUtility

Just for formatting I'm submitting this as an answer.

Couldn't find any good examples comparing them so:

string testString = "http://test# space 123/text?var=val&another=two"; Console.WriteLine("UrlEncode:         " + System.Web.HttpUtility.UrlEncode(testString)); Console.WriteLine("EscapeUriString:   " + Uri.EscapeUriString(testString)); Console.WriteLine("EscapeDataString:  " + Uri.EscapeDataString(testString)); Console.WriteLine("EscapeDataReplace: " + Uri.EscapeDataString(testString).Replace("%20", "+"));  Console.WriteLine("HtmlEncode:        " + System.Web.HttpUtility.HtmlEncode(testString)); Console.WriteLine("UrlPathEncode:     " + System.Web.HttpUtility.UrlPathEncode(testString));  //.Net 4.0+ Console.WriteLine("WebUtility.HtmlEncode: " + WebUtility.HtmlEncode(testString)); //.Net 4.5+ Console.WriteLine("WebUtility.UrlEncode:  " + WebUtility.UrlEncode(testString)); 

Outputs:

UrlEncode:             http%3a%2f%2ftest%23+space+123%2ftext%3fvar%3dval%26another%3dtwo EscapeUriString:       http://test#%20space%20123/text?var=val&another=two EscapeDataString:      http%3A%2F%2Ftest%23%20space%20123%2Ftext%3Fvar%3Dval%26another%3Dtwo EscapeDataReplace:     http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo  HtmlEncode:            http://test# space 123/text?var=val&another=two UrlPathEncode:         http://test#%20space%20123/text?var=val&another=two  //.Net 4.0+ WebUtility.HtmlEncode: http://test# space 123/text?var=val&another=two //.Net 4.5+ WebUtility.UrlEncode:  http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo 

In .Net 4.5+ use WebUtility.UrlEncode

This appears to replicate HttpUtility.UrlEncode (pre-v4.0) for the more common characters:
Uri.EscapeDataString(testString).Replace("%20", "+").Replace("'", "%27").Replace("~", "%7E")
Note: EscapeUriString will keep a valid uri string, which causes it to use as many plaintext characters as possible.

See this answer for a Table Comparing the various Encodings:
https://stackoverflow.com/a/11236038/555798

Line Breaks All of them listed here (other than HttpUtility.HtmlEncode) will convert "\n\r" into %0a%0d or %0A%0D

Please feel free to edit this and add new characters to my test string, or leave them in the comments and I'll edit it.

like image 39
Thymine Avatar answered Sep 27 '22 19:09

Thymine