c#: UrlDecode()

1、源起:

KV需要解析从插件传来的URL网址,因为其可能经过编码,所以需要解码。

初用System.Web.HttpUtility.UrlDecode()这个函数,但根据用户环境crash场景,发现有.net framework库不全情况,即找不到System.Web.dll这个程序集。

居然有此等事情!

自写代码解析喽,在网上下载得System.Web源代码,抽离所需函数,满足需求,项目可以舍弃对System.Web的引用

2、UrlDecode(string str)

直贴代码以做备忘:

        public static string UrlDecode(string str)
        {
            return UrlDecode(str, Encoding.UTF8);
        }

        static void WriteCharBytes(IList buf, char ch, Encoding e)
        {
            if (ch > 255)
            {
                foreach (byte b in e.GetBytes(new char[] { ch }))
                    buf.Add(b);
            }
            else
                buf.Add((byte)ch);
        }

        static int GetInt(byte b)
        {
            char c = (char)b;
            if (c >= ‘0‘ && c <= ‘9‘)
                return c - ‘0‘;

            if (c >= ‘a‘ && c <= ‘f‘)
                return c - ‘a‘ + 10;

            if (c >= ‘A‘ && c <= ‘F‘)
                return c - ‘A‘ + 10;

            return -1;
        }

        static int GetChar(string str, int offset, int length)
        {
            int val = 0;
            int end = length + offset;
            for (int i = offset; i < end; i++)
            {
                char c = str[i];
                if (c > 127)
                    return -1;

                int current = GetInt((byte)c);
                if (current == -1)
                    return -1;
                val = (val << 4) + current;
            }

            return val;
        }

        static string UrlDecode(string s, Encoding e)
        {
            if (null == s)
                return null;

            if (s.IndexOf(‘%‘) == -1 && s.IndexOf(‘+‘) == -1)
                return s;

            if (e == null)
                e = Encoding.UTF8;

            long len = s.Length;
            var bytes = new List<byte>();
            int xchar;
            char ch;

            for (int i = 0; i < len; i++)
                ch = s[i];
                if (ch == ‘%‘ && i + 2 < len && s[i + 1] != ‘%‘)
                {
                    if (s[i + 1] == ‘u‘ && i + 5 < len)
                    {
                        // unicode hex sequence
                        xchar = GetChar(s, i + 2, 4);
                        if (xchar != -1)
                        {
                            WriteCharBytes(bytes, (char)xchar, e);
                            i += 5;
                        }
                        else
                            WriteCharBytes(bytes, ‘%‘, e);
                    }
                    else if ((xchar = GetChar(s, i + 1, 2)) != -1)
                    {
                        WriteCharBytes(bytes, (char)xchar, e);
                        i += 2;
                    }
                    else
                    {
                        WriteCharBytes(bytes, ‘%‘, e);
                    }
                    continue;
                }

                if (ch == ‘+‘)
                    WriteCharBytes(bytes, ‘ ‘, e);
                else
                    WriteCharBytes(bytes, ch, e);
            }

            byte[] buf = bytes.ToArray();
            bytes = null;
            return e.GetString(buf);
        }

3、简单验证

    string url = "https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLeghS9MH9xY";
    url = UrlDecode(url);
    Console.WriteLine(url);  //url = https://www.youtube.com/watch?v=LeghS9MH9xY
时间: 2024-08-10 21:29:57

c#: UrlDecode()的相关文章

那些年我们一起挖掘SQL注入 - 2.全局防护Bypass之UrlDecode

0x01 背景 现在的WEB程序基本都有对SQL注入的全局过滤,像PHP开启了GPC或者在全局文件common.php上使用addslashes()函数对接收的参数进行过滤,尤其是单引号.遇到这种情况我们就需要找一些编码解码的函数来绕过全局防护,这篇文章讲urldecode()的情况,同样大牛请自觉绕道~漏洞来源于乌云:http://www.wooyun.org/bugs/wooyun-2014-050338 0x02 环境搭建 看背景我们使用了低版本的easytalk程序,版本为X2.4①源码

C++ URLDecode和URLEncode实现

转载--http://blog.163.com/zhangjie_0303/blog/static/9908270620148251658993/ #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; typedef unsigned char BYTE; inline BYTE toHex(const BYTE &x) { return x > 9

HtmlEncode、HtmlDecode、UrlEncode、UrlDecode

HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码.例如:"<".">"."&" 等. HtmlDecode: 把经过 HtmlEncode编码过的字符解码 ,还原成原始字符. UrlEncode: 将 Url 中不允许出现的字符进行编码.例如:":"."/"."?" 等. UrlDecode: 把经过 UrllEncode编码过的字符解码 ,还原

再说php urlencode urldecode

说多了都是泪,curl调用远程api接口, client端进行urlencode, 在server端urldecode后确实错的, 翻了一下php的手册,原来php对超全局变量$_GET $_REQUEST已经做了解码操作了,无需再次解码. ps:$_COOKIE需要urldecode!!!

php中urldecode()和urlencode()和stripslashes() 的作用

urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%. urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其原理就是把十六进制字符串转换为中文字符 stripslashes() 函数 实例 删除反斜杠: <?php echo stripslashes("Who\'s Bill Gates?"); ?> 运行实例 定义和用法 stripslashes() 函数删除由 addslashe

C# URL编码转换 URL转码 UrlDecode UrlEncode

using System.Web; 引用system.web. textBox2.Text = System.Web.HttpUtility.UrlDecode(textBox1.Text, System.Text.Encoding.GetEncoding("GB2312"));//将Url中的编码转换为简体汉字 textBox2.Text = System.Web.HttpUtility.UrlEncode(textBox1.Text, System.Text.Encoding.Ge

Server.UrlEncode、HttpUtility.UrlDecode的区别

在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?测试:string file="文件上(传)篇.doc";string Server_UrlEncode=Server.UrlEncode(file);string Server_UrlDecode=Server.UrlDecode(Server_UrlEncode);string HttpUtility_UrlEncode=System.Web.HttpUtility.UrlEncode(file);string HttpU

js中escape对应的C#解码函数 UrlDecode

js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s),使用过程中有以下几点需要注意 js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码 需要注意的几点: 1.HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法. 2.Server是HttpServ

Web容器自己主动对HTTP请求中參数进行URLDecode处理

这篇文章转载自 : Web容器自己主动对HTTP请求中參数进行URLDecode处理 如题.在Java中或许非常多人都没有注意到当我们发送一个http请求时,假设附带的參数被URLEncode之后,到达web容器之后,开发人员获取到的參数值会自己主动变成了encode之前的值.这是一个非常好的特点,开发人员全然能够忽略http的參数是否须要decode这样的事,可是decode究竟是在什么发生的呢? 第一步就是从request.getParameter()方法下手,可是ServletReques