分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

对编码内容多次UrlDecode

发布时间:2023-09-06 02:18责任编辑:熊小新关键词:暂无标签

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

原文地址:https://www.cnblogs.com/Tpf386/p/9804735.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved