Solution to Chinese garbled characters when values are passed through URL parameters on the Asp.net page

Source: Internet
Author: User

During ASP. NET development, page garbled characters are frequently encountered. The following information is collected on the Internet. For more information, see:

The following reproduced from: http://blog.csdn.net/echsys/archive/2008/03/12/2174125.aspx

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 Parameters " ;
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 Parameters " ;
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 " ));
// We recommend that you use the final method. If you get Chinese parameters from other pages without garbled characters, it will be simpler.

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 request
Httpwebrequest myhttpwebrequest = (Httpwebrequest) webrequest. Create (Message );
// Accept 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 ();

 

The above is an excerpt from the Internet written to the blog for backup .. (* ^__ ^ *) Xi ......

Related Article

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.