C # URL Auto-decode solution

Source: Internet
Author: User

Yesterday afternoon, easy Bao maintenance personnel contact me, ask me three order status, they are deducted the fee is successful, we this is still the order to charge status, after inspection found the signature of the two sides have a problem, carefully see the three orders have a common, user name contains Chinese, then the right remedy ...

Since the ePRO after the successful notice to us, the string with gb2312 UrlEncode, and our interface is implemented with. NET, so the default encoding format is Utf-8, so after receiving the request, the contents of the string Utf-8 UrlDecode, Cause my Side Httputility.urldecode (request["user_name"), encoding.getencoding ("GB2312") obtained data is garbled.

Now the problem to find the reason, how to modify it ...

I looked at the entire request object and found that the parameters obtained in the Request.Url.Query were the most primitive values that were not decoded before the URL and then changed.

1  Public StaticNameValueCollection getquerystring (stringqueryString, Encoding Encoding)2         {3queryString = Querystring.replace ("?","");4NameValueCollection result =NewNameValueCollection (stringcomparer.ordinalignorecase);5             if(!string. IsNullOrEmpty (queryString))6             {7                 intCount =querystring.length;8                  for(inti =0; I < count; i++)9                 {Ten                     intStartIndex =i; One                     intindex =-1; A                      while(I <count) -                     { -                         Charitem =Querystring[i]; the                         if(Item = ='=') -                         { -                             if(Index <0) -                             { +index =i; -                             } +                         } A                         Else if(Item = ='&') at                         { -                              Break; -                         } -i++; -                     } -                     stringKey =NULL; in                     stringValue =NULL; -                     if(Index >=0) to                     { +Key = Querystring.substring (StartIndex, index-startIndex); -Value = querystring.substring (index +1, (I-index)-1); the                     } *                     Else $                     {Panax NotoginsengKey = Querystring.substring (StartIndex, I-startIndex); -                     } the  +Result[key] =Httputility.urldecode (value, encoding); A  the                     if((i = = (Count-1)) && (querystring[i] = ='&')) +                     { -Result[key] =string. Empty; $                     } $                 } -             } -             returnresult; the         } - WuyiNameValueCollection col = getquerystring (Request.Url.Query, Encoding.GetEncoding ("GB2312")); the         stringuser_id = col["user_name"];

C # URL Auto-decode solution

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.