c#: UrlDecode()

來源:互聯網
上載者:User

標籤:寫代碼   getch   函數   crash   work   []   current   net   style   

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

c#: UrlDecode()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.