ASP.NET. Request.Form

  • 2020-05-12 02:28:25
  • OfStack

background

When it comes to the communication between the two websites, A has 1 page a, and by submitting the form, the value is transferred to b of B. The website A system 1 is encoded with UTF-8, and the website B system 1 is encoded with GB2312.

Settings encoded in web.config
Site A: < globalization requestEncoding="UTF-8" responseEncoding="UTF-8" / >
Site B: < globalization requestEncoding="gb2312" responseEncoding="gb2312" / >
To solve

Search results on the Internet are nothing more than modifying the requestEncoding, responseEncoding and so on of the web.config file, which, to me, is a bad idea. In addition, the network frantically copy, paste, did not verify the feasibility of the behavior, 1 straight make my hair hair, directly lead to the search results of thousands of 1 law, found is that a few articles, the most angry is that the content is wrong. So I 1 straight insist that the problem, really verified the feasibility, just posted in the blog, so as not to mistake people. (I hope I didn't get too far.)

In fact, this process is very simple, that is, a page of UTF-8 sends data to the page of GB2312. Due to different codes, it is normal to have scrambled codes, so the solution is to use the same code.

The encoding of the web page, 1 is the encoding for receiving the request data (requestEncoding) and 1 is the encoding for sending the response (responseEncoding). The scrambled code problem can be solved by having the a page send the code for the response and the b page receive the code 1 for the request.

Add Response.ContentEncoding = Encoding.GetEncoding ("gb2312") to the a page Page_Load method. Can.
 
//  Set up the a The page response is coded as gb2312 , guarantee and b The code in which the page receives the request 1 to  
protected void Page_Load(object sender, EventArgs e) 
{ 
Response.ContentEncoding = Encoding.GetEncoding("gb2312"); 
} 

You might have a problem with it, and say, well, why don't you go back and set the code for the b page to receive the request and the code for the a page to send the response? I have tried, no way, see the code below, if you know the principle, please let me know, thank you very much!
 
//  Set up the b The code of page receiving request is utf-8 , guarantee and a Page response coding 1 But it won't work. Why?  
protected void Page_Load(object sender, EventArgs e) 
{ 
Request.ContentEncoding = Encoding.GetEncoding("utf-8"); 
} 

Related articles: