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

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

博客园 - 迷你软件

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();
        }