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:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>
<Body>
<Form id = "myForm" action = "http: // localhost: 9000/WebForm1.aspx" method = "post">
Name: <input tyep = "text" name = "name" width = "200px" value = "Single-node cold river fishing"/>
<Br/>
Age: <input tyep = "text" name = "age" width = "200px" value = "24"/>
<Br/>
<Br/>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
In this HTML file, we use GB2312 encoding. The Form contains the name and age data. First, set method to GET:
<Form id = "myForm" action = "http: // localhost: 9000/WebForm1.aspx" method = "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 http: // localhost: 9000/WebForm1.aspx 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 ["name"]
Request. Params ["name"]
Request. QueryString ["name"]
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:
<Form id = "myForm" action = "http: // localhost: 9000/WebForm1.aspx" method = "POST">
When you click the "Submit" button, you can obtain the webpage parameters in WebForm1. The specific methods are as follows:
Request ["name"]
Request. Params ["name"]
Request. Form ["name"]
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:
StringBuilder sb = new StringBuilder ();
IServiceProvider provider = (IServiceProvider) HttpContext. Current;
HttpWorkerRequest worker = (HttpWorkerRequest) provider. GetService (typeof (HttpWorkerRequest ));
Byte [] bs = worker. GetQueryStringRawBytes ();
String queryString = Encoding. GetEncoding ("GB2312"). GetString (bs );
NameValueCollection querys = HttpUtility. ParseQueryString (queryString, Encoding. GetEncoding ("GB2312 "));
Foreach (var item in querys. Keys)
{
Sb. AppendFormat ("{0 }:{ 1} <br/>", item. ToString (), querys [item. ToString ()]);
}
Solution to problem 2:
// Obtain InputStream
System. IO. Stream str = Request. InputStream;
Int32 strLen, strRead;
StrLen = Convert. ToInt32 (str. Length );
Byte [] strArr = new byte [strLen];
StrRead = str. Read (strArr, 0, strLen );
String queryString = HttpUtility. UrlDecode (strArr, System. Text. Encoding. GetEncoding ("GB2312 "));
NameValueCollection querys = HttpUtility. ParseQueryString (queryString, Encoding. GetEncoding ("GB2312 "));
Foreach (var item in querys. Keys)
{
Sb. AppendFormat ("{0 }:{ 1} <br/>", item. ToString (), querys [item. ToString ()]);
}
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.