我在解码带有参数的Base64编码的URL时遇到过这个困难。
eg: http://www.example.com/Movements.aspx?fno=hello&vol=Bits & Pieces
我的预期结果应该是: 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");
实际结果: fno = hello vol = Bits
我已经搜索了stackoverlow,似乎我需要添加一个自定义算法来解析解码的字符串。但是,由于实际的URL比本例中显示的要复杂得多,我最好向专家请教一个替代解决方案!
Tks阅读!
发布于 2013-06-10 20:53:47
如果URL编码正确,您将拥有:
http://www.example.com/Movements.aspx?fno=hello&vol=Bits+%26+Pieces
%26是&的url编码交易记录
和空格将替换为+
在JS中,使用escape
来正确地编码您的url!
编辑
使用encodeURIComponent
而不是escape
,因为就像Sani Huttunen所说的,不推荐使用‘encodeURIComponent
’。抱歉的!
发布于 2013-06-10 21:04:18
您的查询字符串需要正确编码。Base64不是正确的方式。请改用encodeURIComponent
。您应该分别对每个值进行编码(尽管示例中的大多数部分都不需要):
var qs = "&" + encodeURIComponent("fno") + "=" + encodeURIComponent("hello") + "&" + encodeURIComponent("vol") + "=" + encodeURIComponent("Bits & Pieces");
// Result: "&fno=hello&vol=Bits%20%26%20Pieces"
这样你就不需要在C#中进行Base64解码了。
var qs = HttpUtility.ParseQueryString(Request.Url.Query.Replace("?", ""));
var fileno = qs.Get("fno");
var vol = sq.Get("vol");
https://stackoverflow.com/questions/17024273
复制相似问题