John Roa
John Roa

Reputation: 61

Decoding querystring values on the server side

I'm running a web service on my server using WCF and .Net 4. The contract type is WebGet. Here is my problem, at one point in time, someone was sending data through the querystring that was URL encoded. So I added HttpUtility.UrlDecode to decode the parameters. I think that fixed my issue at the time. Now, I've sent a URL encoded string to it and I see that the string is being URL decoded coming into the method (before even getting to the HttpUtility.UrlDecode).

So now I'm confused, if the .Net code is decoding it before it gets to my method, why would I need to call on decode explicitly? But for a time it wasn't, so is this a recent change to the underlying .Net framework?

My problem now is that my users are sending data (unencoded), where the data looks like this: "abc%1234" and I'm getting "abc34", the decoding is eating 3 characters. However, if I urlencode the % sign to be "abc%251234", the value coming into the method is "abc%1234" (what I expected) and then the call to HttpUtility.UrlDecode is changing it to "abc34" (which is not what I expected).

I'm not sure how to proceed here. Do I rip out the explicit call to URLDecode until it starts coming across encoded again or is there a better way to handle this?

Upvotes: 2

Views: 1447

Answers (1)

EdSF
EdSF

Reputation: 12341

It's a subtle thing in documentation, easily missed:

HttpRequest.QueryString Property

Property Value
NameValueCollection
The query string variables sent by the client. Keys and values are URL-decoded.

So if you access the query string via HttpRequest.QueryString (or Params) collection they are already decoded.

You can get to the raw string in RawUrl, QueryString.ToString() (manually that is - re: manipulation, split, etc.).


End of day, %:

Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI.

REF: RFC3986

Hth

Upvotes: 2

Related Questions