15

How can we encode a string using the URL (RFC 1738) standard in C#?

The following online tool is converting the strings using this standard http://www.freeformatter.com/url-encoder.html

An example of the string I want to convert is test(brackets) and the encoded string should look like:

test%28brackets%29
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rumi
  • 3,293
  • 12
  • 68
  • 109
  • Possible duplicate of [URL Encoding using C#](https://stackoverflow.com/questions/575440/url-encoding-using-c-sharp) – dey.shin Feb 16 '18 at 19:33

3 Answers3

26

Uri.EscapeDataString does what you want. See MSDN.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Thomas Hetzer
  • 1,537
  • 13
  • 24
  • Sorry It does not work. Tried this string str = "test(brackets)"; str = Uri.EscapeDataString(str); – rumi Feb 25 '14 at 12:31
  • Strange. When I try "string result = Uri.EscapeDataString("test(brackets)");" the result is "test%28brackets%29". What value has str after EscapeDataString on your system? – Thomas Hetzer Feb 25 '14 at 12:38
  • The string stays the same. It does not change – rumi Feb 25 '14 at 12:41
  • 7
    The behaviour of Uri.EscapeDataString [changed with .NET 4.5](http://msdn.microsoft.com/en-us/library/hh367887%28v=vs.110%29.aspx) to include RFC 3986 characters. – Dirk Feb 25 '14 at 12:43
  • @Dirk: Thanks for the info. Indeed I am targeting .NET 4.5. – Thomas Hetzer Feb 25 '14 at 12:46
  • Tried, change from 4.0 to 4.5 in project properties does not work, new one 4.5 web app will work. A little strange. – Lei Yang Dec 29 '15 at 02:06
9

According to RFC 1738:

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
reserved characters used for their reserved purposes may be used
unencoded within a URL.

Neither HttpUtility.UrlEncode nor WebUtility.UrlEncode will encode those characters since the standard says the parentheses () can be used unencoded.

I don't know why the URL Encoder / Decoder you linked encodes them since it also lists them as as a character that can be used in a URL.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Dirk
  • 10,668
  • 2
  • 35
  • 49
  • I m making calls to a third party API over HTTP and passing a string in the parameters. This string is UTF-8 URL encoded. My API client is written in asp.net C# where as the API host is probably written in Java. When I have characters like parenthesis/brackets () in the string parameter, UTF-8 encoder does not encode them whereas the API host encodes them in %28 and %29 and I get incorrect response. Any suggestions how to fix this? – rumi Feb 25 '14 at 12:49
  • The API has the following reference in the documentation http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html – rumi Feb 25 '14 at 12:51
  • You could maybe look at this: http://stackoverflow.com/questions/846487/how-to-get-uri-escapedatastring-to-comply-with-rfc-3986 – Dirk Feb 25 '14 at 12:52
0

Uri.EscapeDataString will convert the string using the Uri standrards, which is not RFC 1738 conform.

RFC 1738 is an old URL standard.
I accomplished it by using FormUrlEncodedContent:

data = new List<KeyValuePair<string, string>>();
data.Add(new KeyValuePair<string, string>("key", "value"));

var payloadBody = await new FormUrlEncodedContent(data).ReadAsStringAsync();

If you don`t need a encoded URL body you probably need to trick arround with the key / value f.e. let the value empty.

Genfood
  • 1,436
  • 3
  • 15
  • 26