c# decodeuri,用C#进行URL编码

d07e0ba8e777093e667475e8a7149038.png

DIEA

我一直在试验.NET为URL编码提供的各种方法。下表可能很有用(作为我编写的测试应用程序的输出):Unencoded UrlEncoded UrlEncodedUnicode UrlPathEncoded EscapedDataString EscapedUriString HtmlEncoded HtmlAttributeEncoded HexEscapedA         A          A                 A              A                 A                A           A                    %41B         B          B                 B              B                 B                B           B                    %42a         a          a                 a              a                 a                a           a                    %61b         b          b                 b              b                 b                b           b                    %620         0          0                 0              0                 0                0           0                    %301         1          1                 1              1                 1                1           1                    %31[space]   +          +                 %20            %20               %20              [space]     [space]              %20!         !          !                 !              !                 !                !           !                    %21"         %22        %22               "              %22               %22              "      "               %22#         %23        %23               #              %23               #                #           #                    %23$         %24        %24               $              %24               $                $           $                    %24%         %25        %25               %              %25               %25              %           %                    %25&         %26        %26               &              %26               &                &       &                %26'         %27        %27               '              '                 '                '       '                %27(         (          (                 (              (                 (                (           (                    %28)         )          )                 )              )                 )                )           )                    %29*         *          *                 *              %2A               *                *           *                    %2A+         %2b        %2b               +              %2B               +                +           +                    %2B,         %2c        %2c               ,              %2C               ,                ,           ,                    %2C-         -          -                 -              -                 -                -           -                    %2D.         .          .                 .              .                 .                .           .                    %2E/         %2f        %2f               /              %2F               /                /           /                    %2F:         %3a        %3a               :              %3A               :                :           :                    %3A;         %3b        %3b               ;              %3B               ;                ;           ;                    %3B         %3e        %3e               >              %3E               %3E              >        >                    %3E?         %3f        %3f               ?              %3F               ?                ?           ?                    %3F@         %40        %40               @              %40               @                @           @                    %40[         %5b        %5b               [              %5B               %5B              [           [                    %5B\         %5c        %5c               \              %5C               %5C              \           \                    %5C]         %5d        %5d               ]              %5D               %5D              ]           ]                    %5D^         %5e        %5e               ^              %5E               %5E              ^           ^                    %5E_         _          _                 _              _                 _                _           _                    %5F`         %60        %60               `              %60               %60              `           `                    %60{         %7b        %7b               {              %7B               %7B              {           {                    %7B|         %7c        %7c               |              %7C               %7C              |           |                    %7C}         %7d        %7d               }              %7D               %7D              }           }                    %7D~         %7e        %7e               ~              ~                 ~                ~           ~                    %7EĀ         %c4%80     %u0100            %c4%80         %C4%80            %C4%80           Ā           Ā                    [OoR]ā         %c4%81     %u0101            %c4%81         %C4%81            %C4%81           ā           ā                    [OoR]Ē         %c4%92     %u0112            %c4%92         %C4%92            %C4%92           Ē           Ē                    [OoR]ē         %c4%93     %u0113            %c4%93         %C4%93            %C4%93           ē           ē                    [OoR]Ī         %c4%aa     %u012a            %c4%aa         %C4%AA            %C4%AA           Ī           Ī                    [OoR]ī         %c4%ab     %u012b            %c4%ab         %C4%AB            %C4%AB           ī           ī                    [OoR]Ō         %c5%8c     %u014c            %c5%8c         %C5%8C            %C5%8C           Ō           Ō                    [OoR]ō         %c5%8d     %u014d            %c5%8d         %C5%8D            %C5%8D           ō           ō                    [OoR]Ū         %c5%aa     %u016a            %c5%aa         %C5%AA            %C5%AA           Ū           Ū                    [OoR]ū         %c5%ab     %u016b            %c5%ab         %C5%AB            %C5%AB           ū           ū                    [OoR]这些列表示编码如下:UrlEncode:HttpUtility.UrlEncodeUrlEncodedUnicode:HttpUtility.UrlEncodeUnicodeUrlPathEncode:HttpUtility.UrlPathEncodeEscapedDataString:Uri.EscapeDataString替罪羊UriString:Uri.EscapeUriStringHtmlEncode:HttpUtility.HtmlEncodeHtmlAttributeEncode:HttpUtility.HtmlAttributeEncode半身:Uri.HexEscape注:HexEscape只能处理前255个字符。因此它抛出一个ArgumentOutOfRange拉丁文A的例外-扩展字符(例如)。此表是在.NET 4.0中生成的(请参阅LeviBotelho下面的注释,说明.NET 4.5中的编码略有不同)。编辑:我添加了第二个表,其中包含.NET 4.5的编码。见这个答案:https://stackoverflow.com/a/21771206/216440编辑2:由于人们似乎很欣赏这些表,我想您可能会喜欢生成表的源代码,这样您就可以自己玩了。它是一个简单的C#控制台应用程序,可以针对.NET 4.0或4.5:using System;using System.Collections.Generic;using System.Text;// Need to add a Reference to the System.Web assembly.using System.Web;

namespace UriEncodingDEMO2{

    class Program

    {

        static void Main(string[] args)

        {

            EncodeStrings();

            Console.WriteLine();

            Console.WriteLine("Press any key to continue...");

            Console.Read();

        }

        public static void EncodeStrings()

        {

            string stringToEncode = "ABCD" + "abcd"

            + "0123" + " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + "ĀāĒēĪīŌōŪū";

            // Need to set the console encoding to display non-ASCII characters correctly (eg the 

            //  Latin A-Extended characters such as ĀāĒē...).

            Console.OutputEncoding = Encoding.UTF8;

            // Will also need to set the console font (in the console Properties dialog) to a font 

            //  that displays the extended character set correctly.

            // The following fonts all display the extended characters correctly:

            //  Consolas

            //  DejaVu Sana Mono

            //  Lucida Console

            // Also, in the console Properties, set the Screen Buffer Size and the Window Size 

            //  Width properties to at least 140 characters, to display the full width of the 

            //  table that is generated.

            Dictionary> columnDetails =

                new Dictionary>();

            columnDetails.Add("Unencoded", (unencodedString => unencodedString));

            columnDetails.Add("UrlEncoded",

                (unencodedString => HttpUtility.UrlEncode(unencodedString)));

            columnDetails.Add("UrlEncodedUnicode",

                (unencodedString => HttpUtility.UrlEncodeUnicode(unencodedString)));

            columnDetails.Add("UrlPathEncoded",

                (unencodedString => HttpUtility.UrlPathEncode(unencodedString)));

            columnDetails.Add("EscapedDataString",

                (unencodedString => Uri.EscapeDataString(unencodedString)));

            columnDetails.Add("EscapedUriString",

                (unencodedString => Uri.EscapeUriString(unencodedString)));

            columnDetails.Add("HtmlEncoded",

                (unencodedString => HttpUtility.HtmlEncode(unencodedString)));

            columnDetails.Add("HtmlAttributeEncoded",

                (unencodedString => HttpUtility.HtmlAttributeEncode(unencodedString)));

            columnDetails.Add("HexEscaped",

                (unencodedString                    =>

                    {

                        // Uri.HexEscape can only handle the first 255 characters so for the 

                        //  Latin A-Extended characters, such as A, it will throw an 

                        //  ArgumentOutOfRange exception.                       

                        try

                        {

                            return Uri.HexEscape(unencodedString.ToCharArray()[0]);

                        }

                        catch

                        {

                            return "[OoR]";

                        }

                    }));

            char[] charactersToEncode = stringToEncode.ToCharArray();

            string[] stringCharactersToEncode = Array.ConvertAll(charactersToEncode,

                (character => character.ToString()));

            DisplayCharacterTable(stringCharactersToEncode, columnDetails);

        }

        private static void DisplayCharacterTable(TUnencoded[] unencodedArray,

            Dictionary> mappings)

        {

            foreach (string key in mappings.Keys)

            {

                Console.Write(key.Replace(" ", "[space]") + " ");

            }

            Console.WriteLine();

            foreach (TUnencoded unencodedObject in unencodedArray)

            {

                string stringCharToEncode = unencodedObject.ToString();

                foreach (string columnHeader in mappings.Keys)

                {

                    int columnWidth = columnHeader.Length + 1;

                    Func encoder = mappings[columnHeader];

                    string encodedString = encoder(unencodedObject);

                    // ASSUMPTION: Column header will always be wider than encoded string.

                    Console.Write(encodedString.Replace(" ", "[space]").PadRight(columnWidth));

                }

                Console.WriteLine();

            }

        }

    }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值