0

I have met this difficulty while decoding a Base64 encoded URL with parameters

eg: http://www.example.com/Movements.aspx?fno=hello&vol=Bits & Pieces

My expected results should be: fno = hello vol = Bits & Pieces

#Encoding:
//JAVASCRIPT                
var base64 = $.base64.encode("&fno=hello&vol=Bits & Pieces");
window.location.replace("Movements.aspx?" + base64);

#Decoding c#
string decodedUrl = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(Request.Url.Query.Replace("?", ""))); // Replace is used to remove the ? part from the query string. 
string fileno = HttpUtility.ParseQueryString(decodedUrl).Get("fno");
string vol = HttpUtility.ParseQueryString(decodedUrl).Get("vol");

Actual Result: fno = hello vol = Bits

I have searched stackoverlow and seems I need to add a custom algorithm to parse the decoded string. But as the actual URL is more complicated than shown in this example I taought better asks experts for an alternative solution!

Tks for reading!

FunMatters
  • 593
  • 1
  • 10
  • 26

2 Answers2

1

If the URL were correctly encoded, you would have :

http://www.example.com/Movements.aspx?fno=hello&vol=Bits+%26+Pieces

%26 is the url encoded caract for &
and spaces will be replaced by +

In JS, use escape to encode correctly your url!

[EDIT]

Use encodeURIComponent instead of escape because like Sani Huttunen says, 'escape' is deprecated. Sorry!

JoDev
  • 6,633
  • 1
  • 22
  • 37
1

Your querystring needs to be properly encoded. Base64 is not the correct way. Use encodeURIComponent instead. you should encode each value separately (although not needed in most parts in the example):

var qs = "&" + encodeURIComponent("fno") + "=" + encodeURIComponent("hello") + "&" + encodeURIComponent("vol") + "=" + encodeURIComponent("Bits & Pieces");
// Result: "&fno=hello&vol=Bits%20%26%20Pieces"

Then you don't need to Base64 decode in C#.

var qs = HttpUtility.ParseQueryString(Request.Url.Query.Replace("?", ""));
var fileno = qs.Get("fno");
var vol = sq.Get("vol");
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • Thanks Sani. You were spot on ! I was encoding the whole query part. Your example triggered my wrong doings! Base64 is not actually necessary here but as it was implemented in the whole project I might use it as well. – FunMatters Jun 11 '13 at 04:59