In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to solve the Web garbled problem, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
There are two ways to submit Web data: GET and POST. For an introduction to these two methods, see here: differences in Get/Post requests for Http. What I want to introduce here is how to get HTTPRequest data in the program and successfully solve the problem of garbled code caused by different codes.
Now let's start with a piece of HTML code:
Untitled document name: age:
In this HTML file, the encoding we use is that the GB2312,Form form contains both name and age data. First set method to the GET method:
In addition, we create a new Web application, create a new site locally, set the port to 9000, and add a page named WebForm1.aspx, which is the address http://localhost:9000/WebForm1.aspx pointed to by the action in the Form form above.
When you click the "submit" button, you can get the parameters of the web page in WebForm1 in the following ways:
Request ["name"] Request.Params ["name"] Request.QueryString ["name"]
The strings obtained by these three methods are converted by default, because the coding defaults to UTF-8 when we use vs to build the project, so garbled will occur. This is a kind of problem, and we will solve it later.
Next, set method to the POST method:
When you click the "submit" button, you can get the parameters of the web page in WebForm1 in the following ways:
Request ["name"] Request.Params ["name"] Request.Form ["name"]
The same problem as * *, garbled codes will also occur here after the default UTF-8 conversion. This is the second question.
The solution to problem one:
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}", item.ToString (), querys [item.ToString ()]) }
The solution to problem 2:
/ get InputStream System.IO.Stream str = Request.InputStream; Int32 strLen, strRead; strLen = Convert.ToInt32 (str.Length); byte [] strArr = new byte [strLen]; strstrRead = 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}", item.ToString (), querys [item.ToString ()]);}
In addition, for * methods, you can decode the URL directly with GB2312, and the code is no longer posted here.
With these two methods, no matter what kind of garbled code, you can rest easy.
The above content is how to solve the problem of Web garbled. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.