Web garbled Solution

Source: Internet
Author: User

 

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.

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.