ASP tutorial the request receiving parameter garbled solution in. Net
asp.net tutorial Request object's properties and methods are more, commonly used several are: UserAgent back to the client browser version information, UserHostAddress sent back to remote client machine host IP address, userhostname Returns the DNS name of the remote client machine, Physicalapplicationpath returns the true path to the current request Web page on the server side.
Here's the point, my Web.config configuration file is configured with the global "gb2312" encoding:
<globalization requestencoding= "gb2312" responseencoding= "gb2312" fileencoding= "gb2312" culture= "ZH-CN"/>
The current Taiwan submitted "Chinese text", the background with request.querystring["XXX"] received is garbled.
No matter how System.web.httputility.urldecode ("XXX", "Encoding type") is used, the decoding is invalid
Principle Explanation:
1: The first thing to determine is: The client's URL parameter at the time of submission, ext.js will be encoded and then submitted, and the client's encoding defaults to UTF-8 encoding
The client defaults to three encoding functions: Escape () encodeURI () encodeURIComponent ()
2: Then why use the request.querystring["XXX"] to receive parameters, the received will be garbled?
To do this, we must unravel the original process logic of Request.QueryString
Cut into: querystringencoding
Internal encoding querystringencoding
{
Get
{
Encoding contentencoding = this.contentencoding;
if (!contentencoding.equals (Encoding.unicode))
{
return contentencoding;
}
return Encoding.UTF8;
}
}
From the querystringencoding code, the system defaults to the Globalization configuration node encoding method, if not, the default is UTF-8 encoding
The conclusion came out.
When the client JS to Chinese to utf-8 encoding to the server, with Request.QueryString receive, will first globalization configuration gb2312 to decode once, so, produced a garbled.
All the causes are:
1:JS encoding mode is urt-8
2: The server is configured with the default gb2312
3:request.querystring The default will call the Httputility.urldecode to decode the receive parameters with the system configuration code.
The road to the solution
Knowing the principles, there are a variety of ways to solve them:
1: Overall unified for utf-8 coding, easy and worry.
2: Global specified gb2312 encoding, the URL with Chinese, JS is not encoded, such as ext.js frame.
This way you can only special processing, in the service end to specify the encoding and decoding,
Because the default system calls a Httputility.urldecode ("XXX", the system-configured encoding),
So you call again Httputility.urlencode ("XXX", the system-configured encoding), and return to the original URT-8 encoding parameter
Then use Httputility.urldecode ("xxx", utf-8) and decode it.
5: Other Note: The default for a decoding of the URI also includes the property, and Request.rawurl is the original parameter