Recently, I 've been so frustrated by garbled code. Now I have come to the end of my work. I want to summarize the situation of garbled data transmitted on the Web. I hope that friends who are also troubled by garbled code will be able to fall asleep with peace of mind!
There are two ways to submit Web data: GET and POST. For more information about the two methods, see the difference between Http Get/Post requests. Here I will introduce how to get HTTPRequest data in the program and solve the problem of garbled characters caused by inconsistent encoding.
Now let's start with a piece of HTML code:
Name: Age:
In this HTML file, we use GB2312 encoding. The Form contains the name and age data. First, set method to GET:
In addition, we will create a new Web application, create a local site, set the port to 9000, and add a page named WebForm1.aspx, that is, the address pointed to by the action in the Form above
When you click the "Submit" button, you can obtain the webpage parameters in WebForm1. The specific methods are as follows:
Request. Params [Request. QueryString [
The strings obtained by these three methods are converted by default encoding, because we use vs to build a project, the encoding is the UTF-8 by default, so then garbled. This is the first problem. We will solve it later.
Next, set the method to the post method:
When you click the "Submit" button, you can obtain the webpage parameters in WebForm1. The specific methods are as follows:
Request. Params [Request. Form [
As with the first type of problem, garbled characters appear here after the default UTF-8 conversion. This is the second problem.
Solution to problem 1:
IServiceProvider provider HttpWorkerRequest worker String queryString NameValueCollection querys
{
Sb. AppendFormat (}
Solution to problem 2:
Int32 strLen, strRead;
StrLen strRead
NameValueCollection querys
{
Sb. AppendFormat (}
In addition, for the first method, you can use GB2312 to decode the URL directly.
With these two methods, no matter how garbled, you can rest assured.