对编码内容多次UrlDecode
Posted tpf386
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对编码内容多次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);
下一步应该怎么办,只能希望某个大牛指点下了……
以上是关于对编码内容多次UrlDecode的主要内容,如果未能解决你的问题,请参考以下文章
php中urldecode()和urlencode()起什么作用
shell 下 urlencode/urldecode 编码/解码的方法