How to URL encode strings in C#
Asked Answered
G

3

16

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
Gelman answered 25/2, 2014 at 12:13 Comment(2)
Possible duplicate of URL Encoding using C#Selfappointed
You also can "convert" brackets manually with Replace methodHafer
S
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.

Samuella answered 25/2, 2014 at 12:22 Comment(3)
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?Gelman
The API has the following reference in the documentation docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.htmlGelman
You could maybe look at this: #846987Samuella
R
27

Uri.EscapeDataString does what you want. See MSDN.

Ruel answered 25/2, 2014 at 12:28 Comment(6)
Sorry It does not work. Tried this string str = "test(brackets)"; str = Uri.EscapeDataString(str);Gelman
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?Ruel
The string stays the same. It does not changeGelman
The behaviour of Uri.EscapeDataString changed with .NET 4.5 to include RFC 3986 characters.Samuella
@Dirk: Thanks for the info. Indeed I am targeting .NET 4.5.Ruel
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.Briggs
S
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.

Samuella answered 25/2, 2014 at 12:22 Comment(3)
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?Gelman
The API has the following reference in the documentation docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.htmlGelman
You could maybe look at this: #846987Samuella
U
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.

Urson answered 16/3, 2020 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.