免费在线URL编码器和解码器
URLs 不能包含空格、& 符号或原始 unicode 字符 - 它们需要百分比编码。 Dokall 对字符串进行编码以便在 URLs 中安全使用,并将编码值解码回纯文本。
对于构建查询字符串、调试重定向 URLs 以及处理 Web 应用程序中的国际字符至关重要。
What is URL Encoding?
URL encoding (also called percent-encoding) converts characters that are not allowed or have special meaning in URLs into a safe format. Each unsafe character is replaced with a percent sign followed by two hexadecimal digits representing the character's ASCII or UTF-8 byte value.
For example, a space becomes %20, an ampersand (&) becomes %26, and a forward slash (/) becomes %2F. The Japanese character becomes %E6%97%A5 (three bytes in UTF-8, each percent-encoded). This ensures that URLs remain valid and unambiguous regardless of what data they carry.
Reserved and Unreserved Characters
URLs have two character categories. Unreserved characters can appear anywhere in a URL without encoding: A-Z, a-z, 0-9, hyphen (-), period (.), underscore (_), and tilde (~). These are always safe.
Reserved characters have special meaning in URL syntax: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. For example, ? starts the query string, & separates query parameters, and # starts the fragment. When these characters are used as data (not as delimiters), they must be percent-encoded.
The tricky part: whether a character needs encoding depends on where it appears. A / in the path component separates directories (do not encode). A / inside a query parameter value is data (encode it). This context-dependent encoding is why there are different encoding functions.
encodeURI vs encodeURIComponent in JavaScript
JavaScript provides two encoding functions with different purposes.
encodeURI() encodes a full URI. It preserves characters that are valid in a URI: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. Use it when you have a complete URL and want to encode only the characters that are never valid in any URL position (spaces, non-ASCII characters, etc.).
encodeURIComponent() encodes a URI component - a piece of a URL like a query parameter value. It encodes all reserved characters including & = ? / because these could conflict with the URL structure. Use it when building query strings: "?search=" + encodeURIComponent(userInput).
Common mistake: using encodeURI() on user input in a query string. If the user input contains &, it will not be encoded, and the browser will interpret it as a parameter separator. Always use encodeURIComponent() for query parameter values.
URL Encoding in Different Languages
In Python: urllib.parse.quote() encodes a string component (like encodeURIComponent). urllib.parse.quote_plus() encodes spaces as + instead of %20 (used in application/x-www-form-urlencoded data). urllib.parse.urlencode() encodes a dictionary into a query string.
In Java: URLEncoder.encode(str, "UTF-8") encodes for form data (spaces become +). For path encoding, use URI class constructors or a library like Apache Commons HttpClient.
In Go: url.QueryEscape() encodes for query strings (spaces become +). url.PathEscape() encodes for path segments (spaces become %20). The net/url package provides URL parsing and construction that handles encoding automatically.
In PHP: urlencode() encodes spaces as + (form encoding). rawurlencode() encodes spaces as %20 (RFC 3986). Use rawurlencode() for path components and urlencode() for form data.
Spaces in URLs: %20 vs + and When Each Applies
There are two ways to encode a space in a URL, and which one is correct depends on the context.
%20 is the standard percent-encoding for a space, defined in RFC 3986. It is correct in all URL components - path segments, query parameter names, query parameter values, and fragments.
+ is an alternative encoding for spaces, but only in the application/x-www-form-urlencoded content type - the format used by HTML form submissions. In this context (and only this context), spaces are encoded as + and + is encoded as %2B.
When building URLs for API calls, use %20. When submitting HTML forms or posting form-encoded data, + is acceptable. Most servers and frameworks handle both, but using the correct encoding avoids edge cases and ambiguity.