对编码内容多次UrlDecode

 

对编码内容多次UrlDecode,并不会影响最终结果。

尝试阅读了微软的源代码,不过不容易读懂。

网址:https://referencesource.microsoft.com/#System/net/System/Net/WebUtility.cs,73c04b8a4fde5039

以下为从网址上复制下来的一些关键代码,不过没看懂。

public static string UrlDecode(string encodedValue)
        {
            if (encodedValue == null)
                return null;
 
            return UrlDecodeInternal(encodedValue, Encoding.UTF8);
        }

 

复制代码
private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (value == null)
            {
                return null;
            }
 
            int count = value.Length;
            UrlDecoder helper = new UrlDecoder(count, encoding);
 
            // go through the string's chars collapsing %XX and
            // appending each char as char, with exception of %XX constructs
            // that are appended as bytes
 
            for (int pos = 0; pos < count; pos++)
            {
                char ch = value[pos];
 
                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    int h1 = HexToInt(value[pos + 1]);
                    int h2 = HexToInt(value[pos + 2]);
 
                    if (h1 >= 0 && h2 >= 0)
                    {     // valid 2 hex chars
                        byte b = (byte)((h1 << 4) | h2);
                        pos += 2;
 
                        // don't add as char
                        helper.AddByte(b);
                        continue;
                    }
                }
 
                if ((ch & 0xFF80) == 0)
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                else
                    helper.AddChar(ch);
            }
 
            return helper.GetString();
        }
复制代码

 

 internal void AddChar(char ch)
            {
                if (_numBytes > 0)
                    FlushBytes();
 
                _charBuffer[_numChars++] = ch;
            }

 

  internal void AddByte(byte b)
            {
                if (_byteBuffer == null)
                    _byteBuffer = new byte[_bufferSize];
 
                _byteBuffer[_numBytes++] = b;
            }

 

复制代码
 internal String GetString()
            {
                if (_numBytes > 0)
                    FlushBytes();
 
                if (_numChars > 0)
                    return new String(_charBuffer, 0, _numChars);
                else
                    return String.Empty;
            }
复制代码

 

String(_charBuffer, 0, _numChars);是看不到源代码的,到这里已经变成一行看不懂的代码
        // Creates a new string from the characters in a subarray.  The new string will
        // be created from the characters in value between startIndex and
        // startIndex + length - 1.
        //
        [System.Security.SecuritySafeCritical]  // auto-generated
        [ResourceExposure(ResourceScope.None)]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        public extern String(char [] value, int startIndex, int length);

下一步应该怎么办,只能希望某个大牛指点下了……

 

posted on   荆棘人  阅读(1398)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示