7

I type the following url into my web browser and press enter.

http://localhost/website.aspx?paymentID=6++7d6CZRKY%3D&language=English

Now in my code when I do HttpContext.Current.Request.QueryString["paymentID"],

I get 6 7d6CZRKY=

but when I do HttpContext.Current.Request.QueryString.ToString() I see the following:

paymentID=6++7d6CZRKY%3D&language=English

The thing I want to extract the actual payment id that the user typed in the web browser URL. I am not worried as to whether the url is encoded or not. Because I know there is a weird thing going on here %3D and + sign at the same time ! But I do need the actual + sign. Somehow it gets decoded to space when I do HttpContext.Current.Request.QueryString["paymentID"].

I just want to extract the actual payment ID that the user typed. What's the best way to do it?

Thank you.

Varun Sharma
  • 2,591
  • 8
  • 45
  • 63

3 Answers3

8

You'll need to encode the URL first, using URLEncode(). + in URL equals a space so needs to be encoded to %2b.

string paymentId = Server.UrlEncode("6++7d6CZRKY=");
// paymentId = 6%2b%2b7d6CZRKY%3d

And now

string result = Request.QueryString["paymentId"].ToString();
//result = 6++7d6CZRKY=

However

string paymentId = Server.UrlEncode("6  7d6CZRKY=");
//paymentId looks like you want it, but the + is a space -- 6++7d6CZRKY%3d

string result = Request.QueryString["paymentId"].ToString();
//result = 6 7d6CZRKY=
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • Thanks. I am not sure if the URL that is in HttpContext.Current.Request is encoded or not! So as of now I just wrote a function to extract the payment id value out of it (Francis's approach is using regular expression). – Varun Sharma Nov 19 '12 at 00:19
  • 1
    Probably want to find that out. Other method is not recommended. – MikeSmithDev Nov 19 '12 at 00:23
  • Actually the url which we receive is not in correct format. It has a + symbol in it and our client does mean that's its a plus symbol. They should have put %2B instead of +. And now we need to come up with some kind of hacky solution. – Varun Sharma Nov 21 '12 at 20:53
2

There is some info on this here: Plus sign in query string.

But I suppose you could also use a regular expression to get your parameter out of the query string. Something like this:

string queryString = HttpContext.Current.Request.QueryString.ToString();
string paramPaymentID = Regex.Match(queryString, "paymentID=([^&]+)").Groups[1].Value;
Community
  • 1
  • 1
Francis Gagnon
  • 3,545
  • 1
  • 16
  • 25
1

I sent an Arabic text in my query string

Arabic text in my query string

and when I resieved this string it was Encoded enter image description here

after Server.UrlDecode

 departmentName = Server.UrlDecode(departmentName);

it back again to arabic enter image description here

I hope this help you

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92