encoding.url Package
Function Description
The url package provides URL-related capabilities, including parsing URL components, encoding and decoding URLs, and combining URLs or paths.
Uniform Resource Locator (URL) is an address used to identify the location of a resource on the Internet. A URL includes protocol, host name, path, and query parameters. The protocol refers to the protocol (such as HTTP and FTP) used to access resources. The host name refers to the domain name or IP address of the server where the resource is located. The path refers to the specific location of the resource. The query parameter is a string used to transfer parameters. The URL is the only way to identify resources on the Internet, and can be used to access various resources such as web pages, images, and videos.
Generally, the URL is in the following format:
scheme://host[:port]/path[?query][#fragment]
In the preceding format:
scheme
: protocol, such ashttp
,https
, andftp
host
: host name or IP addressport
: port number, which is optional and is the default port number of the protocol by defaultpath
: resource path, for example,/index.html
and/blog/post/123
query
: query parameter, which is optional, for example,?page=2&sort=desc
fragment
: document fragment identifier, which is optional, for example,#section-1
For example, the URL of https://www.example.com/blog/post/123?page=2&sort=desc#section-1
is in the following format:
- scheme: https
- host:
www.example.com
- path: /blog/post/123
- query: ?page=2&sort=desc
- fragment: #section-1
The reason and basic process of URL encoding are as follows:
URL encoding is a process of converting non-ASCII characters in a URL to ASCII characters with better readability. This is because only ASCII characters are allowed in a URL. Non-ASCII characters may cause URL parsing errors or transmission failures.
The URL encoding process is as follows:
- Convert a URL string to a byte array.
- Convert each non-
ASCII
character to aUTF-8
encoded byte sequence. - Convert each byte to two hexadecimal numbers.
- Add a percent sign (%) before each hexadecimal number.
- Concatenate all encoded characters to form an encoded URL string.
For example, if URL encoding is performed on the string "Hello, World!", the result is "%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81".
API List
Class
Name | Description |
---|---|
Form | Stores HTTP request parameters in key-value pairs. A key can correspond to multiple values, and the values are stored in arrays. |
URL | Provides functions used to parse URLs and other related functions. |
UserInfo | Indicates the username and password in the URL. |
Exception Class
Name | Description |
---|---|
UrlSyntaxException | Specifies a URL parsing exception class. |