惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
V
Visual Studio Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
The Cloudflare Blog
B
Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
Know Your Adversary
Know Your Adversary
I
InfoQ
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
C
Cisco Blogs
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
月光博客
月光博客
博客园 - Franky
Project Zero
Project Zero
G
Google Developers Blog
S
SegmentFault 最新的问题
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
S
Security @ Cisco Blogs
Martin Fowler
Martin Fowler
A
Arctic Wolf
T
Tenable Blog
L
LINUX DO - 最新话题
TaoSecurity Blog
TaoSecurity Blog
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 迷你软件

MySQL 主从同步延迟监控 Test post from Metablog Api 博文阅读密码验证 - 博客园 ASP.NET中的Menu控件在谷歌浏览器下显示异常的解决办法 符合标准的正常工作的对联广告(漂浮广告JS代码) C#:按钮颜色设置 用相对定位实现简单的图片边框阴影效果 如何识别 SQL Server 的版本 2010最危险的编程错误(转) NopCommerce学习:MSSQL 2005 排序规则导致中文编码错误 获取本机MAC地址 IPv4 to Integer C# 用SQL语句执行存储过程 1.3.6.1.2.1 - SNMP MIB-2 MIB MODULE HOST-RESOURCES-MIB Simple SNMP with SimpleSnmp 浅谈多线程中数据的绑定和赋值 - 迷你软件 - 博客园 SNMP基础简介 SNMP windows OIDs
URL解码时,为什么将加号解码为空?
迷你软件 · 2010-01-29 · via 博客园 - 迷你软件

以下代码在.NET Framework 2.0中测试。 

先看一个例子:


                    
//将“A+B”编码后赋给变量parameters
                    string parameters = Server.UrlEncode("A+B");//此时,变量 parameters 的值为 "A%2bB"
                    
                    
//将该参数传递给测试页面
                    Response.Redirect("test.aspx?p=" + parameters);

 test.aspx页面: 


        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                
if (Request.QueryString["p"!= null)
                {
                    
//获取Url参数的值
                    string parameters = Request.QueryString["p"].ToString();//Url解码
                    parameters = Server.UrlDecode(parameters);//此时,参数 parameters 的值为???

                    Response.Write(parameters);
                }
            }
        }

 当参数 parameters 输出到页面后,值已经不为“A+B”了,而变成“A B”,将“+”变成了空格。

原因:

在test.aspx页面,“Request.QueryString”中的值仍然是“A%2bB”,但通过Request.QueryString["p"]取值时,得到的结果已经被自动解码了(注意

那么在“string parameters = Request.QueryString["p"].ToString();”条语句执行完后,parameters 的值已经为“A+B”了。

然后再执行“parameters = Server.UrlDecode(parameters);”条语句时,等于对“A+B”进行了解码。

解码后的值变成了“A B”(A空格B)。 

疑问:

“A+B”解码后为什么变成"A B"(将“+”解码为空格)?  请高手赐教。

 以下代码来源于.NET Framework 2.0,红色显示部分为造成该问题的原因。


        
// .NET Framework 2.0 中的代码
        private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
        {
            
int length = s.Length;
            UrlDecoder decoder = new UrlDecoder(length, e);
            
for (int i = 0; i < length; i++)
            {
                
char ch = s[i];
                
if (ch == '+')
                {
                    ch = ' ';
                }
                
else if ((ch == '%'&& (i < (length - 2)))
                {
                    
if ((s[i + 1== 'u'&& (i < (length - 5)))
                    {
                        
int num3 = HexToInt(s[i + 2]);
                        
int num4 = HexToInt(s[i + 3]);
                        
int num5 = HexToInt(s[i + 4]);
                        
int num6 = HexToInt(s[i + 5]);
                        
if (((num3 < 0|| (num4 < 0)) || ((num5 < 0|| (num6 < 0)))
                        {
                            
goto Label_0106;
                        }
                        ch = (char)((((num3 << 12| (num4 << 8)) | (num5 << 4)) | num6);
                        i += 5;
                        decoder.AddChar(ch);
                        
continue;
                    }
                    
int num7 = HexToInt(s[i + 1]);
                    
int num8 = HexToInt(s[i + 2]);
                    
if ((num7 >= 0&& (num8 >= 0))
                    {
                        
byte b = (byte)((num7 << 4| num8);
                        i += 2;
                        decoder.AddByte(b);
                        
continue;
                    }
                }
            Label_0106:
                
if ((ch & 0xff80== 0)
                {
                    decoder.AddByte((byte)ch);
                }
                
else
                {
                    decoder.AddChar(ch);
                }
            }
            
return decoder.GetString();
        }