对编码内容多次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);

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

 

转载于:https://www.cnblogs.com/Tpf386/p/9804735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中进行URL解码操作可以使用urllib.parse库中的unquote函数。以下是一个示例代码: ``` import urllib.parse def urldecode(url): decoded_url = urllib.parse.unquote(url) return decoded_url # 示例用法 encoded_url = "http%3A%2F%2Fwww.baidu.com" decoded_url = urldecode(encoded_url) print(decoded_url) ``` 在这个示例中,我们导入了urllib.parse库,并定义了一个名为urldecode的函数。该函数接受一个经过URL编码的字符串作为参数,并使用urllib.parse.unquote函数对其进行解码。最后,我们使用示例值进行测试,并打印解码后的URL。 所以,要在Python中进行URL解码操作,你可以使用urllib.parse.unquote函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 中的 urlencode 编码urldecode 解码](https://blog.csdn.net/csdnzouqi/article/details/123394132)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python常用函数(urlencode 与 urldecode)](https://blog.csdn.net/m0_59485658/article/details/128265688)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值