0

I have to create a webrequest using HTTP GET and I have to add on the url a JSON serialized and encoded with all parameters.

But I have a problem when I'm encoding the serialized object, they encode also symbols like "{" and ":"

I would like to know what I have to do in order to encode the serialized object like the followed above:

Serialized Object:

{\"Name\":\"Bob\"}

Encoded With HttpUtility.Utility, or another encoder will encode all symbols:like "{" ":"

"%7b%22Name%22%3a%22Bob%22%2

What I'm looking for:

http://tmpserviceURL.test?parameters={%20%22Name%22:%20%22Bob%22}
2
  • 1
    You want those characters to be encoded. Commented Nov 24, 2014 at 13:37
  • ty Ant, I don't want to encode "{" and ":" Commented Nov 24, 2014 at 13:50

1 Answer 1

2

@Ant P is correct: you want those characters to be encoded. It is a bad idea not to encode them.

HttpUtility.UrlEncode and other similar methods encode {, } and : because they MUST do so per section 2.2 of the Uniform Resource Locators specification (RFC 1738).

From page 2:

Octets [within a URL] must be encoded if they have no corresponding graphic character within the US-ASCII coded character set, if the use of the corresponding character is unsafe, or if the corresponding character is reserved for some other interpretation within the particular URL scheme.

The spec goes on to define : as being in the set of "reserved" characters (those that have special meaning within a URL), and defines { and } as being in the set of "unsafe" characters (those that are known to be sometimes modified by gateways and other transport agents).

So, in short, if you send these characters unencoded in a URL, then you risk the URL not being interpretted correctly, or the data being corrupted by the time it reaches its destination. It may work sometimes, but you can't rely on it always working.

If you really feel you must ignore the URL spec, then you will have to roll your own URL encoder that does not encode those particular characters. I doubt you're going to find an off-the-shelf encoder that allows you to do this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.