Search code examples
asp.netvb.netquery-stringrequest-queueing

asp.net querystring formatting


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


Solution

  • 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.