C #: UrlDecode ()

Source: Internet
Author: User

1, from the source:

KV needs to parse the URL URLs that came from the plugin because it may be encoded, so it needs to be decoded.

First Use System.Web.HttpUtility.UrlDecode () this function, but according to the user environment crash scene, found that there is a. NET Framework library is not the case, that is, System.Web.dll this assembly is not found.

There is such a thing!

Self-written code parsing, download the system.web source code on the Internet, pull away the required functions, to meet the requirements, the project can discard the reference to System.Web

2, UrlDecode (String str)

Direct code to make memo:

         Public Static stringUrlDecode (stringstr) {            returnUrlDecode (str, ENCODING.UTF8); }        Static voidWritecharbytes (IList buf,Charch, Encoding e) {            if(Ch >255)            {                foreach(byteBinchE.getbytes (New Char[] {ch}) Buf.            ADD (b); }            Elsebuf. ADD (byte) ch); }        Static intGetInt (byteb) {Charc = (Char) b; if(c >='0'&& C <='9')                returnC'0'; if(c >='a'&& C <='F')                returnC'a'+Ten; if(c >='A'&& C <='F')                returnC'A'+Ten; return-1; }        Static intGetChar (stringStrintOffsetintlength) {            intval =0; intEnd = length +offset;  for(inti = offset; I < end; i++)            {                Charc =Str[i]; if(C >127)                    return-1; intCurrent = GetInt ((byte) c); if(Current = =-1)                    return-1; Val= (Val <<4) +Current ; }            returnVal; }        Static stringUrlDecode (strings, Encoding e) {            if(NULL==s)return NULL; if(S.indexof ('%') == -1&& S.indexof ('+') == -1)                returns; if(E = =NULL) e=Encoding.UTF8; LongLen =s.length; varbytes =Newlist<byte>(); intXchar; Charch;  for(inti =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 SequenceXchar = GetChar (s, i +2,4); if(Xchar! =-1) {writecharbytes (bytes, (Char) Xchar, E); I+=5; }                        Elsewritecharbytes (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); Elsewritecharbytes (Bytes, ch, e); }            byte[] buf =bytes.            ToArray (); Bytes=NULL; returne.getstring (BUF); }

3. Simple verification

    string " Https%3a%2f%2fwww.youtube.com%2fwatch%3fv%3dleghs9mh9xy " ;     = urldecode (URL);    Console.WriteLine (URL);   //  Https://www.youtube.com/watch?v=LeghS9MH9xY

C #: UrlDecode ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.