asp.net querystring formatting

2k views Asked by At

How can i send special characters in a querystring?

Like:

thankyou.aspx?data=GQH/FUvq9sQbWwrYh5xX7G++VktXU5o17hycAfNSND8gt8YbbUaJbwRw

The ++ gets taken out when i do this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim theData As String = Request.QueryString("data")
  ....

It stores it inside theData like this:

GQH/FUvq9sQbWwrYh5xX7G VktXU5o17hycAfNSND8gt8YbbUaJbwRw

So therefore its invalid. How can i properly send that string over without it changing once its received?

update

Doing this:

Dim en As String = endecryption.EncryptData("=" & "aclub" & "=" & strName & "=" & strEmail)
Response.Redirect("/thankyou.aspx?data=" & HttpUtility.UrlEncode(en), False)

And at the other end:

Dim theData As String = HttpUtility.UrlDecode(Request.QueryString("data"))

It sends it like: GQH%2fFUvq9sQbWwrYh5xX7G%2bVktXU5o17hycAfNSND8gt8YbbUaJbwRw

But it decodes it like: GQH/FUvq9sQbWwrYh5xX7G[2 spaces here]VktXU5o17hycAfNSND8gt8YbbUaJbwRw

2

There are 2 answers

5
Mike Corcoran On BEST ANSWER

take a look at using Server.UrlEncode() to encode the param before including it in the querystring, and using Server.UrlDecode() to transform it back when you need it.

msdn article

calling Server.UrlEncode() on "GQH/FUvq9sQbWwrYh5xX7G++VktXU5o17hycAfNSND8gt8YbbUaJbwRw" yields:

GQH%2fFUvq9sQbWwrYh5xX7G%2b%2bVktXU5o17hycAfNSND8gt8YbbUaJbwRw

and calling Server.UrlDecode on that result yields:

GQH/FUvq9sQbWwrYh5xX7G++VktXU5o17hycAfNSND8gt8YbbUaJbwRw

You need to make sure that you url encode the query string data before you append it on the url.

1
EdSF On

You can:

  1. As Mike stated, you can HttpServerUtility.UrlEncode the base64 string above (and conversely decode)
  2. If you're sticking with MS on both ends, you can also look into HttpServerUtility.UrlTokenEncode - see this note if you choose this option

Update

Ugh. It's coming back to me - base64 gotchas: "The constructor for HttpRequest will parse the actual string QueryString and UrlDecode the values for you. Be careful not to DOUBLE DECODE." - Scott Hanselman

My personal suggestion would be to go the UrlToken route if you're going to do base64...