URL编码/解码工具
在线URL编码解码工具,帮助您安全地处理含特殊字符的URL,确保网址传输的安全性。
常见问题
什么是 URL 编码?
URL 编码是一种将字符转换为可以通过互联网传输的格式的方法。URL 只能使用 ASCII 字符集通过互联网发送。由于 URL 经常包含 ASCII 集之外的字符,因此需要转换 URL。URL 编码将不安全的 ASCII 字符替换为 '%' 后跟两个十六进制数字。
URL 编码如何工作?
URL 编码通过将不安全字符替换为 '%' 后跟表示该字符 UTF-8 编码的两个十六进制数字来工作。例如,空格字符编码为 %20。URL 不能包含空格,因此它们被替换为加号 (+) 或 %20。其他特殊字符则被替换为对应的 %xx 代码。
常见需要 URL 编码的字符
以下是在 URL 中使用时需要 URL 编码的常见字符:
保留字符
| 字符 | URL编码 | 说明 |
|---|---|---|
| # | %23 | 哈希符号(用于URL片段) |
| $ | %24 | 美元符号 |
| & | %26 | 与符号(URL参数分隔符) |
| + | %2B | 加号 |
| , | %2C | 逗号 |
| / | %2F | 斜杠(URL路径分隔符) |
| : | %3A | 冒号 |
| ; | %3B | 分号 |
| = | %3D | 等号(URL参数赋值符号) |
| ? | %3F | 问号(URL查询字符串开始符号) |
| @ | %40 | at符号 |
| [ | %5B | 左方括号 |
| ] | %5D | 右方括号 |
其他常用字符
| 字符 | URL编码 | 说明 |
|---|---|---|
| Space | %20 | URL中最常见的需要编码的字符 |
| ! | %21 | 感叹号 |
| " | %22 | 双引号 |
| % | %25 | 百分号(URL编码的转义字符) |
| ' | %27 | 单引号 |
| ( | %28 | 左括号 |
| ) | %29 | 右括号 |
| * | %2A | 星号 |
| \ | %5C | 反斜杠 |
| ^ | %5E | 脱字符 |
| { | %7B | 左花括号 |
| | | %7C | 竖线 |
| } | %7D | 右花括号 |
| ~ | %7E | 波浪号 |
不同编程语言中的 URL 编码实现
以下是各种编程语言中 URL 编码和解码的示例:
Frontend / Scripting
JavaScript
// URL encoding
const text = "Hello World! Special chars: &?=/";
const encoded = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded = decodeURIComponent(encoded);
console.log("Decoded:", decoded);TypeScript
// URL encoding
const text: string = "Hello World! Special chars: &?=/";
const encoded: string = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded: string = decodeURIComponent(encoded);
console.log("Decoded:", decoded);Python
import urllib.parse
# URL encoding
text = "Hello World! Special chars: &?=/"
encoded = urllib.parse.quote(text)
print(f"Encoded: {encoded}")
# URL decoding
decoded = urllib.parse.unquote(encoded)
print(f"Decoded: {decoded}")Backend / Systems
Go
package main
import (
"fmt"
"net/url"
)
func main() {
// Encode a URL
text := "Hello World! Special chars: &?=/";
encoded := url.QueryEscape(text)
fmt.Println("Encoded:", encoded)
// Decode a URL
decoded, err := url.QueryUnescape(encoded)
if err == nil {
fmt.Println("Decoded:", decoded)
}
}PHP
<?php
// URL encoding
$text = "Hello World! Special chars: &?=/";
$encoded = urlencode($text);
echo "Encoded: " . $encoded . "\n";
// URL decoding
$decoded = urldecode($encoded);
echo "Decoded: " . $decoded . "\n";
?>C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function to URL-encode a string
char *url_encode(char *str) {
char *encoded = malloc(strlen(str) * 3 + 1);
char *pstr = str;
char *pbuf = encoded;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
*pbuf++ = *pstr;
} else if (*pstr == ' ') {
*pbuf++ = '+';
} else {
sprintf(pbuf, "%%%.2X", *pstr);
pbuf += 3;
}
pstr++;
}
*pbuf = '\0';
return encoded;
}
int main() {
char *text = "Hello World! Special chars: &?=/";
char *encoded = url_encode(text);
printf("Original: %s\n", text);
printf("Encoded: %s\n", encoded);
free(encoded);
return 0;
}encodeURI 和 encodeURIComponent 有什么区别?
encodeURI() 适用于编码完整的 URI,因此不会对 URL 中具有特殊含义的字符进行编码,例如 /、?、:、@、&、=、+、$ 和 #。 相比之下,encodeURIComponent() 会对所有特殊字符进行编码,适用于对 URL 的部分内容进行编码,例如查询字符串参数。 在编码查询参数时,应始终使用 encodeURIComponent(),以确保所有特殊字符都被正确编码。
URL 编码排错清单
二次编码
如果看到 %2520,说明 %20 里的百分号又被编码成了 %25。重新编码前先解码一次,确认真实值。
空格是 + 还是 %20
HTML 表单编码常把空格转成 +,而 URL 组件编码通常使用 %20。做签名、缓存 key 或接口比对前,要确认后端期望哪一种。
路径与查询值
斜杠在路径中有结构含义,但在查询参数值里通常应该编码。应编码单个参数值,而不是已经拼好的完整 URL。
Unicode 参数
中文、emoji 和其他非 ASCII 文本会先转成 UTF-8 字节,再做百分号编码。乱码通常意味着原始文本用了其他字符编码。
URL 编码实用场景
更稳妥的做法是在拼接查询字符串之前先编码参数值。下面这些例子说明应该编码哪一段,以及为什么要编码。
带空格的搜索词
- 原始值
q=hello world- 编码后
q=hello%20world
查询值中的空格需要编码。表单提交有时使用 +,但通用 URL 组件中 %20 更稳妥。
把回调 URL 作为参数
- 原始值
redirect=https://example.com/a?x=1&y=2- 编码后
redirect=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1%26y%3D2
嵌套 URL 的 ? 和 & 必须作为参数值编码,否则会破坏外层查询字符串。
需要保留的加号
- 原始值
phone=+1 555 0100- 编码后
phone=%2B1%20555%200100
未编码的 + 可能被表单解码器当成空格。加号有实际含义时应写成 %2B。
Unicode 关键词
- 原始值
tag=中文✅- 编码后
tag=%E4%B8%AD%E6%96%87%E2%9C%85
非 ASCII 文本会先转成 UTF-8 字节,再进行百分号编码。