1. 程式人生 > >對編碼內容多次UrlDecode

對編碼內容多次UrlDecode

sys () nco each dimp tar class https ren

對編碼內容多次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);

下一步應該怎麽辦,只能希望某個大牛指點下了……

 

對編碼內容多次UrlDecode