asp.net querystring Garbled solution method

Source: Internet
Author: User

 gb2312 Web Site If you are directly using JavaScript for AJAX data submission, can lead to QueryString garbled, the following provides a workaround

Normally, many asp.net sites now use UTF8 for page encoding, which is the same as the code for JavaScript default sites, but there are also a significant number of GB2312   For GB2312 Web sites, if you are submitting Ajax data directly with JavaScript, for example: Http://www.xxx.com/accept.aspx?name= John, Or in the UTF8 Web site with the following ASP.net code to submit, is not good, will lead to QueryString garbled.     Code as follows: WebRequest request = WebRequest.Create ("Http://www.xxx.com/accept.aspx?name= John");   Request. method = "POST";   HttpWebResponse response = (HttpWebResponse) request. GetResponse ();     So under the GB2312 code of the site to get request.querystring["name" is garbled, MS has encoded the code conversion this piece of packaging good.   in the UTF8 code communication and GB2312 Web site communication mode of the code conversion has a variety of implementations:   First: The character to be transmitted first UrlEncode, the encoded characters in the decoding by the UTF8 encoding method for manual decoding, so as to ensure consistent results , even if the transmission to the target page when GB2312, the results are the same, to avoid QueryString garbled. The decoding method is the following code.   Code as follows: Httputility.urldecode (S, Encoding.UTF8);      This will get the right John, This requires that in the submission of the first Httputility.urlencode encoded into UTF8 first, and then put to the name= (encoded characters), which is currently more common and common solution, but the disadvantage of having one is to tell others you first how to URL coding first, and then how.   Second: More alternative, directly read the client-submitted byte data conversion, the reason request.querystring["name" will be garbled, is MS based on the current page encoding conversion caused, such as the current page encoding is GB2312, And the person who submitted is UTF8, you do not employHome to submit the UTF8 code turn of course is garbled, is not someone else passed is garbled. At this time we need to get the original data to decode to avoid querystring garbled, it is very regrettable that I did not find directly to provide the head raw byte data method for us to use, it does not matter, the anatomy of Ms source code, found code as follows:   Code as follows: public NameValueCollection QueryString {            get {                 if (_querystring = null) {                    _Q uerystring = new Httpvaluecollection ();                        if (_WR!= null)       & nbsp                 fillinquerystringcollection ();                        _querystring.makereadonly ();                                   I F (_flags[needtovalidatequerystring]) {                    _flags. Clear (needtovalidatequerystring);                      validatenamevaluecollection (_querystring, "Re Quest. QueryString ");                                  &N Bsp return _querystring;                     {    code as follows: private void Fillinquerystri Ngcollection ()   {      byte[] Querystringbytes = this. Querystringbytes;       if (querystringbytes!= null)       {          if (Querystri) Ngbytes.length!= 0                         This._querystri Ng. Fillfromencodedbytes (Querystringbytes, this. querystringencoding);           {     }       else if (!string. IsNullOrEmpty (this. QueryStringText))       {&nbsp         this._querystring.fillfromstring (this. QueryStringText, True, this. querystringencoding);      }  }       by the way, the QueryString was initialized the first time it was accessed, and if you don't use it in your program, that object will remain empty, MS considers the details   We all saw the Querystringbytes attribute, the prototype is as follows internal byte[] Querystringbytes, this is the original QueryString byte. The moves:     Copy code code as follows: Type type = Request.gettype ();   PropertyInfo property = type. GetProperty ("Querystringbytes",   bindingflags.instance  | Bindingflags.ignorecase | BindingFlags.NonPublic);   byte[] Querybytes = (byte[]) property. GetValue (Request, NULL);   String querystring = Httputility.urldecode (Querybytes, Encoding.UTF8);       and see what QueryString is, haha name= John.   All kinds of coding conversion can be completed on their own, after all, the original byte submitted, I hope to solve the problem of QueryString garbled help.

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.