Beware of traps caused by unencoded URL Transfer Parameters (transcoding)

Source: Internet
Author: User
In recent projects, we have encountered some "inexplicable" errors caused by uncoding (transcoding) When URL parameters are passed, so as to avoid detours like me.

We know that a web project always has to pass parameters like a http://www.google.cn/search? Hl = ZH-CN & Q = % E5 % 95% 8A, which includes two parameters, HL and Q, and Q is encoded ("ah" in Chinese "). But in projects, when we pass parameters, we forget to encode (decode, transcode) the parameters. What kind of traps will this cause?

1, the company's original OA (ASP development, file code: gb2312) and just developed the internal business system (Asp.net development, file code: uft-8) integration, login OA, write the user name into the cookie (cookie ["user_name"]), and then read the cookie in the business system. Why? The file encoding is different, so the cookie name in the business system has changed to "User % 5fname "!

2, the Business System (File Code: uft-8) Internal call to an external PHP file (Code: gb2312) send SMS: http://sms.XXX.cn/index.php? Controller = bosends & Action = sendsms & username = A Company & Password = PASSWORD & mobile = 139 ******** & content = Hello, which means sending fails! Check the PHP file encoding to find out that it is gb2312. When username and password are passed in, no relevant information is found in the system because username ("A company") has changed!

In these two cases, we must transcode the file: for example, 2. httputility. urlencode ("A company", system. text. encoding. getencoding ("gb2312"), of course we can write a method:

Code
///   <Summary>
/// Encoding
///   </Summary>
///   <Param name = "str"> </param>
///   <Param name = "codingtype"> </param>
///   <Returns> </returns>
Public   String Strencoding ( String STR, String Codingtype)
{
Return Httputility. urlencode (STR, system. Text. encoding. getencoding (codingtype ));
}

Then, encode the parameters on the system's internal page:

3. The boss is very angry: XXX. Have you come over to this statistical system?
Me: Yes! Is there any problem?
Boss: What's going on? The link clearly contains statistics. How can I query details of the link? Even the Statistical Chart is invisible!
Me: Let me see...

After checking, I found that the boss is correct, and the link does have statistics, but the detailed data of the link is no longer displayed! ???

Get back to your desk and try the http://www.XXX.com/admins/DayChart.aspx? UN = ABC & UL = http://p.yiqifa.com/servlet/handleClick? SID = a7e226f4bb8f1a18 & pid = 221 & WID = 232

This link needs to pass two parameters: UN and UL, where, UN = ABC, UL = http://p.yiqifa.com/servlet/handleClick? SID = a7e226f4bb8f1a18, but after passing the parameter, what does ul become? Maybe you have discovered: ul = http://p.yiqifa.com/servlet/handleClick? SID = a7e226f4bb8f1a18 & pid = 221 & WID = 232. Yes, UL has become http://p.yiqifa.com/servlet/handleClick? SID = a7e226f4bb8f1a18. PID and WID are treated as parameters of daychart. aspx!

4. collect statistics on all links and image clicks on a page. Click again within 1 minute without warehouse receiving, store user clicks using cookies, and asynchronously transmit data to the server using scripts on the Static Page. Run the application on the local machineProgramThe requirement is met, but there is a problem on the server: Click n multiple different links, only the first two pieces of data will be entered!

Is it a domain name issue? The application on the server uses the domain name, while the local host is localhost. So I modified the host file and simulated the domain name for the local machine. The problem was fixed again. The problem between the local machine and the server was the same, and only two pieces of data were inserted!

LookCodeBecause the cookie operation is special, every time you click to write the cookie to a text file, you can check that the written cookie value is different from the expected value...

The Cookie needs to be written to the client. Is it possible that the encoding of the file is generated? Well! Therefore, before writing a cookie, encode the string, obtain the cookie, and then decode the string in the cookie. The problem is solved. The server can insert data normally...

But why is that error occurring when using a domain name ??? (Look forward ...) 3. 4 encoding and decoding: httputility. urlencode (STR), httputility. urldecode (STR) is attached to the following link: when submitting a JS script page, pay attention to the transmission of Chinese parameters (escape (STR ))
Someone: Ah ??? This is also included... everyone on Earth knows!
Me: Haha!

The above is only the coding problem encountered in the project. I will record it and hope it will be helpful to you. I hope you can tell me the incorrect code. Thank you!

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.