
browse a web, submit a form or call an API interface, we often see character combinations like% E4% B8% AD% E6% 96% 87 in the address bar. This is actually the role of URL encoding. Many people are accustomed to its existence, but do not understand that it is a key mechanism to ensure the normal transmission of network data. This article will disassemble URL encoding in an all-round way from definition, principle, function to practical application, so that you can thoroughly understand the core logic of this network basic technology.
to understand URL encoding in depth, it is first necessary to clarify what it is and its root cause.
1, the essential attributes of URL encoding
URL encoding, also known as percent encoding, is an encoding method that converts characters or special characters that are not allowed in a URL into a standardized format. It belongs to a subset of URI encoding, and its core purpose is to enable various types of characters to be safely transmitted in URLs, avoiding network request failure or data parsing errors due to character format problems.
2, the background
of URL encodingearly URL specifications only allowed the use of English letters, numbers and a few special symbols, such as -, _,.,~, etc. However, with the development of web applications, URLs need to carry non-ASCII characters such as Chinese and Japanese, as well as characters with special meanings such as spaces, &, =, etc. These characters appear directly in the URL will destroy its structure, so URL encoding came into being and became a standard solution to solve the problem of character compatibility.
understand the definition of URL encoding, let's explore its underlying implementation logic to see how it completes character conversion.
1, ASCII character encoding rules
for ASCII characters that are not allowed in the URL specification, such as spaces, <、>, etc., the URL encoding will first convert them to the corresponding ASCII value, then convert the value to two hexadecimal numbers, and finally add the percent sign to the front. For example, the ASCII code for spaces is 32, and the corresponding hexadecimal is 20, so the URL is encoded with% 20; the ASCII code for exclamation marks is 33, hexadecimal is 21, and encoded with% 21.
2 Encoding rules for non-ASCII characters
for non-ASCII characters such as Chinese and Korean, URL encoding will first convert them to byte arrays corresponding to character sets such as UTF-8 or GBK, and then convert each byte to two hexadecimal numbers with a percent sign%. For example, the UTF-8 byte of Chinese "medium" is E4B8AD, so the URL encoding is% E4% B8% AD; if the GBK character set is used, the byte is D6D0, and the encoding is% D6% D0, but UTF-8 is currently the mainstream character set standard for URL encoding.
URL coding is not a technology generated out of thin air, it plays a key role in multiple links of network transmission, and is the basis for ensuring the normal operation of network services.
1, to ensure the legitimacy of the URL format
URL have strict format specifications, once the character does not meet the requirements, the server will not be able to correctly parse the URL structure, will directly return 400 error or other exceptions. URL encoding by converting special characters to the standard format, ensuring that each URL conforms to RFC standards, allowing the server to accurately identify the requested resource path and parameter information.
2 Avoid ambiguity in data transmission
in the query parameters of the URL, &, = and other characters are special symbols used to separate parameters and assignments. If the parameter value itself contains these characters, it will cause ambiguity when the server parses the parameters. For example, if the parameter value is "a & b", the direct transmission will be recognized by the server as two parameters, and after the URL encoding becomes "a% 26b", it can be correctly parsed into a complete parameter value.
3, improve the security of data transmission
URL encoding can prevent some malicious characters from appearing directly in the URL, reducing the probability of security risks such as SQL injection and XSS Cross Site Scripting. Although it cannot completely replace professional security protection mechanisms, as a basic character processing method, it can convert potentially dangerous characters at the first level and add a layer of basic protection to network requests.
understand the principle and function of URL encoding, let's take a look at its specific application in actual network scenarios and feel its practical value.
1, Web form submission scenarios
when we enter Chinese and special symbols in the web form and submit, the browser will automatically encode the URL of the form parameters, and then splice the encoded parameters in the URL or put them in the request body and send them to the server. For example, the search keyword is "Network Technology & Security", which will become "%E7%BD%91%E7%BB%9C%E6%8A%80%E6%9C%AF%20%26%20%E5%AE%89%E5%85%A8" after URL encoding to ensure that the search request can be processed correctly by the server.
2, API interface call scenario
calling a third-party API interface during the development process, many interfaces require parameters to be passed after URL encoding. For example, when calling the map API, the address parameters contain Chinese and special characters. Unencoded direct transmission will lead to interface return parameter errors. After URL encoding, the interface can accurately parse the address information and return the corresponding map data.
To sum up, URL encoding is the basic technology to ensure the normal transmission of network data. By definition, it is a standardized conversion method for special characters. The bottom layer follows the different encoding rules of ASCII and non-ASCII characters. The core function is to ensure the legitimacy of URLs, avoid transmission ambiguity and improve security. It is widely used in scenarios such as form submission and API calls. Mastering the relevant knowledge of URL encoding can help us better troubleshoot network request problems and improve the stability and security of network operations.