asp.net querystring Garbled solution _ Practical skills

Source: Internet
Author: User

Normally, many of ASP.net's web sites now use UTF8 for page encoding, which is the same as the code for JavaScript default sites, but there are quite a few of them using 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.

Copy Code code as follows:

WebRequest request = WebRequest.Create ("Http://www.xxx.com/accept.aspx?name= John");
Request. method = "POST";
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();

In this way, the GB2312 code of the Web site to get request.querystring["name" is garbled, MS has converted the code to the package is good.

There are a number of ways to implement encoding and decoding in UTF8 coded communication and GB2312 Web site communication:

First: The character to be transmitted to the urlencode, the encoded characters in the decoding of UTF8 encoding method for manual decoding, so as to ensure consistent results, even if the target page transmission to GB2312, the results are the same, to avoid QueryString garbled. The decoding method is the following code.

Copy Code code as follows:

Httputility.urldecode (S, Encoding.UTF8);

This can get the correct John, which requires that the Httputility.urlencode encoded into UTF8 first, and then into the name= (encoded characters), which is the current more common and common solution, There is only one drawback is to tell others you first how to URL coding first, and then how.

The 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 others submitted is UTF8, you do not have to submit the UTF8 code is garbled, of course, is not a person passed over 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, does not matter, the anatomy of Ms Source code, found the code as follows:

Copy Code code as follows:

Public NameValueCollection QueryString {
get {
if (_querystring = = null) {
_querystring = new Httpvaluecollection ();

if (_WR!= null)
Fillinquerystringcollection ();

_querystring.makereadonly ();
}

if (_flags[needtovalidatequerystring]) {
_flags. Clear (needtovalidatequerystring);
Validatenamevaluecollection (_querystring, "request.querystring");
}

return _querystring;
}
}

Copy Code code as follows:

private void Fillinquerystringcollection ()  

 & nbsp;  byte[] Querystringbytes = this. querystringbytes; 
    if (querystringbytes!= null)  
    {          if (querystringbytes.length!= 0)  
         { 
            this._ Querystring.fillfromencodedbytes (Querystringbytes, this. querystringencoding); 
       } 
   }  br>    else if (!string. IsNullOrEmpty (this. QueryStringText)  
    { 
        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 see the Querystringbytes attribute, the prototype is 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 code conversion can be completed by themselves, after all, the original byte was 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.