Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

asp.net - asp .net query string encoding and decoding

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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=

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...