There are generally three solutions:
1. Set the web. config file
<System. web>
......
<Globalization requestEncoding = "gb2312" responseEncoding = "gb2312" culture = "zh-CN" fileEncoding = "gb2312"/>
......
</System. web>
2. Before passing Chinese characters, encode the Chinese parameters to be passed and decode them upon receiving them.
> Transfer
String Name = "Chinese parameter ";
Response. Redirect ("B. aspx? Name = "+ Server. UrlEncode (Name ));
> Receive
String Name = Request. QueryString ["Name"];
Response. Write (Server. UrlDecode (Name ));
3. If a Chinese parameter is transferred from the. HTML file to the. Aspx file (that is, Url conversion is not performed using the Redirect () method from the background ). Similarly, the passed Chinese parameters must be encoded and decoded upon receiving.
> Transfer
<Script language = "JavaScript">
Function GoUrl ()
{
Var Name = "Chinese parameter ";
Location. href = "B. aspx? Name = "+ escape (Name );
}
<Body onclick = "GoUrl ()">
> Receive
String Name = Request. QueryString ["Name"];
Response. Write (Server. UrlDecode (Name ));
Summary:
In general. Set the web. config file. However, if you use JavaScript to call the webservice method (passing Chinese parameters to webservice ). The web. config file is invalid.
Or use
Response. Redirect ("test1.aspx? 111 = "+ System. Web. HttpUtility. UrlEncode (" People's Republic of China "));
// It is recommended that you obtain Chinese parameters from other pages without garbled characters.
String message = "http: // localhost/Test/test1.aspx? 111 = "+ System. Web. HttpUtility. UrlEncode (" People's Republic of China ");
Http:
// You need to obtain the address of the return value of a page"
// Send the request
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest. Create (message );
// Accept the request
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest. GetResponse ();
Stream receiveStream = myHttpWebResponse. GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, System. Text. Encoding. GetEncoding ("GB2312 "));
// This is the returned result of the return value of the page to be retrieved.
ReturnValue = readStream. ReadToEnd ();
From: http://blog.csdn.net/faunjoe/archive/2009/09/26/4596826.aspx
Which one should I use for URL encoding? Are there any differences between these two methods?
Test:
String file = "file (upload document .doc ";
String Server_UrlEncode = Server. UrlEncode (file );
String Server_UrlDecode = Server. UrlDecode (Server_UrlEncode );
String HttpUtility_UrlEncode = System. Web. HttpUtility. UrlEncode (file );
String HttpUtility_UrlDecode = System. Web. HttpUtility. UrlDecode (HttpUtility_UrlEncode );
Response. Write ("original data:" + file );
SFun. WriteLine ("Server. UrlEncode:" + Server_UrlEncode );
SFun. WriteLine ("Server. UrlDecode:" + Server_UrlDecode );
SFun. WriteLine ("HttpUtility. UrlEncode:" + HttpUtility_UrlEncode );
SFun. WriteLine ("HttpUtility. UrlDecode:" + HttpUtility_UrlDecode );
Output:
Original data: file (upload document .doc
Server. UrlEncode: performance%c41_bc1_few.c91_cf1_a3%a8%b4% AB %a3%a9%c6%aa.doc
Server. UrlDecode: file (upload example .doc
HttpUtility. UrlEncode: Invalid
HttpUtility. UrlDecode: file (upload example .doc
The difference is that HttpUtility. UrlEncode () is UTF-8 encoded by default, while Server. UrlEncode () is encoded by default.
ASP. when developing pages, we often use System. web. httpUtility. urlEncode and UrlDecode PASS Parameters between pages through URLs. the use of Encode and Decode in pairs is correct.
However, when writing a file download page, we often use the following method to specify the name of the downloaded file:
Response. AddHeader ("Content-Disposition", "attachment; filename ="
+ HttpUtility. UrlEncode (fileName, Encoding. UTF8 ));
The reason for conversion to UTF8 is to support Chinese file names.
This
The problem arises because HttpUtility. UrlEncode converts the space to the plus sign ('+') in the Encode.
The plus sign is converted into a space, but the browser cannot understand the plus sign as a space. Therefore, if the file name contains a space, the space in the file downloaded from the browser is changed to a plus sign.
One solution is to replace "+" with "% 20" after the UrlEncode of HttpUtility (if it is "+", it is converted to "% 2b"), for example:
FileName = HttpUtility. UrlEncode (fileName, Encoding. UTF8 );
FileName = fileName. Replace ("+", "% 20 ");
I don't understand why Microsoft converts spaces to plus signs instead of "% 20". Remember that JDK UrlEncoder converts spaces to "% 20.
After checking, the same is true in. Net 2.0.
The above is copied from other places and well written. I encountered the same problem in one of my own programs. By default, aspx is encoded with UTF-8, in my program, gb2312 must be used as the default encoding.
(<Globalization requestEncoding = "gb2312" responseEncoding = "gb2312"/> ),
Question
The question appears. The value of HttpUtility. UrlDecode returned in Page. Request is garbled.
HttpUtility. UrlDecode uses UTF8 to encode the URL by default. In this case, you only need to change HttpUtility. UrlDecode
Server. UrlEncode.