URL 编解码 使用教程
详细使用指南、最佳实践与常见问题解答
使用场景
URL 编解码工具适用于拼接查询参数、处理含特殊字符的 URL、调试 API 请求、生成安全链接等场景。当你需要把中文、空格、& 等特殊字符放进 URL,或反向解析已编码的 URL 时,这个工具能快速完成转换。
Use Cases
The URL encoder/decoder is useful for building query strings, handling URLs with special characters, debugging API requests, and generating safe links. Use it to put Chinese, spaces, &, and other special characters into URLs, or to parse encoded URLs back.
功能特点
- 双向转换:编码和解码一键切换
- encodeURIComponent:编码所有非 URL 安全字符
- encodeURI:保留 URL 结构字符(:/?&=)
- 实时输出:输入即转换
- 支持中文:正确处理 UTF-8 中文字符
Features
- Bidirectional: encode and decode with one toggle
- encodeURIComponent: encodes all URL-unsafe characters
- encodeURI: preserves URL structure characters (:/?&=)
- Real-time output: converts as you type
- Chinese support: correctly handles UTF-8 Chinese characters
使用示例
示例 1:场景一:API 请求参数含中文,如 `?name=张三&city=北京`,需要编码为 `?name=%E5%BC%A0%E4%B8%89&city=%E5%8C%97%E4%BA%AC` 才能传输。
示例 2:场景二:构造分享链接,包含特殊字符的标题需要编码后拼接到 URL 中。
示例 3:场景三:调试后端日志里看到的编码 URL,粘贴到工具里解码还原成可读中文。
Examples
Example 1: Scenario 1: API request parameters contain Chinese, e.g., `?name=张三&city=北京` must be encoded as `?name=%E5%BC%A0%E4%B8%89&city=%E5%8C%97%E4%BA%AC` for transmission.
Example 2: Scenario 2: Build a share link where the title contains special characters that must be encoded before being appended to the URL.
Example 3: Scenario 3: Debug an encoded URL in backend logs by pasting it into the tool and decoding it back to readable Chinese.
最佳实践
- 查询参数值用 encodeURIComponent,整个 URL 用 encodeURI
- 永远不要手动拼接 URL 参数,用工具或库自动编码
- 解码前确保字符串是有效的编码格式,避免双重解码
- URL 长度建议不超过 2000 字符(浏览器兼容性限制)
Best Practices
- Use encodeURIComponent for query parameter values, encodeURI for entire URLs
- Never manually concatenate URL parameters — use a tool or library to encode automatically
- Ensure the string is valid encoded format before decoding to avoid double-decoding
- Keep URLs under 2000 characters for browser compatibility
常见问题
encodeURIComponent 和 encodeURI 有什么区别?
encodeURIComponent 编码所有非字母数字字符(包括 :/?&=),适合编码单个参数值。encodeURI 保留 URL 结构字符,适合编码完整 URL。
为什么空格编码后是 %20 还是 +?
在 URL 查询字符串中,空格通常用 + 表示(application/x-www-form-urlencoded)。但在 URL 路径中,空格应该用 %20。本工具使用 %20 编码。
能解码已经被编码多次的字符串吗?
能,但需要多次点击"解码"按钮。每次解码会还原一层编码,直到字符串不再变化为止。
FAQ
What is the difference between encodeURIComponent and encodeURI?
encodeURIComponent encodes all non-alphanumeric characters (including :/?&=), suitable for individual parameter values. encodeURI preserves URL structure characters and is suitable for complete URLs.
Why is space encoded as %20 or +?
In URL query strings, spaces are typically represented as + (application/x-www-form-urlencoded). In URL paths, spaces should be %20. This tool uses %20.
Can it decode a string that has been encoded multiple times?
Yes, but you need to click "Decode" multiple times. Each click peels off one layer of encoding until the string stops changing.