URL 编码
高亮为百分号编码字节。
免费在线 URL 编码解码工具,支持 encodeURIComponent / encodeURI / RFC 3986 四种编码模式。编码高亮实时显示被转义字符,批量模式支持多行处理,Query Only 只编码参数值。浏览器本地处理零上传。
高亮为百分号编码字节。
免费在线 URL 编码解码工具,支持 encodeURIComponent / encodeURI / RFC 3986 四种编码模式。编码高亮实时显示被转义字符,批量模式支持多行处理,Query Only 只编码参数值。浏览器本地处理零上传。
encodeURIComponent 会编码所有非安全字符,包括 &、=、? 等,适合编码独立的参数值。encodeURI 保留 URL 结构字符(:、/、?、# 等),适合编码完整 URL。使用场景:参数值用 Component,完整 URL 用 URI。
RFC 3986 是 URL 标准的更新版本,将波浪号 ~ 视为不需编码的安全字符,而旧标准会将其编码为 %7E。使用 RFC 3986 可以避免部分服务端解析差异,兼容性更好。
当你有一个 URL 查询字符串 ?name=张三&city=北京,只想编码参数值而不影响键名和符号时使用。此时 name 和 city 保持不变,值被编码为 %E5%BC%A0%E4%B8%89 和 %E5%8C%97%E4%BA%AC。
在 application/x-www-form-urlencoded 格式中(HTML 表单提交),空格编码为 +。但在 URL 路径和 encodeURIComponent 中,空格始终编码为 %20。本工具默认使用 %20 格式。
这取决于字符编码方式。本工具使用 UTF-8 编码(Web 标准),中文「中」编码为 %E4%B8%AD。如果其他工具使用 GBK 编码,结果会是 %D6%D0。建议统一使用 UTF-8。
高亮显示被编码为 %XX 格式的字符(如空格→%20),帮助理解编码规则和排查问题。英文字母和数字是安全字符不会被编码,中文和特殊符号才会被编码。
需要。% 是 URL 编码的转义起始符,如果在参数值中出现字面量 %,必须编码为 %25,否则会被当作编码前缀导致解析错误。例如 100% 需要编码为 100%25。
URL 编码(Percent-encoding)是将 URL 中的非安全字符转换为 %XX 格式的过程。URL 只能包含一小部分 ASCII 字符(字母、数字、-_.~),其他所有字符(包括中文、空格、特殊符号)都需要编码后才能在 URL 中传输。例如空格编码为 %20,中文「中」编码为 %E4%B8%AD。
**encodeURIComponent vs encodeURI**:这是两个最常用的编码函数。encodeURIComponent 编码最彻底,包括 &、=、? 等参数分隔符,适合编码单个参数值。encodeURI 保留 URL 结构(协议://、路径、/、?、#),只编码真正需要编码的字符,适合编码完整 URL。混淆使用会导致参数解析错误。
**RFC 3986 vs 旧标准**:RFC 3986 将波浪号 ~ 视为安全字符不需编码,而早期标准会编码为 %7E。使用 RFC 3986 可以减少不必要的转义,提高 URL 可读性,也避免某些服务端对 %7E 的解析问题。
**Query Only 模式**:当你只需要编码 URL 中查询参数的值,而不希望影响键名、等号、& 符号时使用。例如 ?name=张三 编码为 ?name=%E5%BC%A0%E4%B8%89,键名保持可读,只编码值部分。
**编码高亮**:实时高亮 %XX 格式的字符,琥珀色标记让用户一眼看出哪些字符被编码。这帮助理解编码规则:英文字母和数字通常是安全字符不会被编码,中文和特殊符号才会被编码。所有计算在浏览器本地完成,数据不上传。
| 字符 | 含义 | encodeURIComponent | encodeURI | RFC 3986 |
|---|---|---|---|---|
空格 | 空格 | %20 | %20 | %20 |
! | 感叹号 | %21 | ! | %21 |
# | 片段标识符 | %23 | # | %23 |
$ | 美元符号 | %24 | $ | %24 |
& | 参数分隔符 | %26 | & | %26 |
' | 单引号 | %27 | ' | %27 |
( | 左括号 | %28 | ( | %28 |
) | 右括号 | %29 | ) | %29 |
* | 星号 | %2A | * | %2A |
+ | 加号 | %2B | + | %2B |
, | 逗号 | %2C | , | %2C |
/ | 路径分隔符 | %2F | / | %2F |
: | 冒号 | %3A | : | %3A |
; | 分号 | %3B | ; | %3B |
= | 等号 | %3D | = | %3D |
? | 查询字符串 | %3F | ? | %3F |
@ | at符号 | %40 | @ | %40 |
~ | 波浪号 | %7E | %7E | ~ |
| 编码后 | 原始字符 | URL 中的用途 |
|---|---|---|
%20 | (空格) | 空格 |
%21 | ! | 子分隔符 |
%23 | # | 片段标识符(hash) |
%25 | % | 编码转义符本身 |
%26 | & | 查询参数分隔符 |
%2B | + | 加号/表单空格 |
%2F | / | 路径分隔符 |
%3A | : | 协议分隔符 |
%3D | = | 参数键值分隔符 |
%3F | ? | 查询字符串起始 |
%40 | @ | 认证/邮箱分隔符 |
%5B | [ | 数组参数(RFC 3986) |
// encodeURIComponent:编码单个参数值(最常用)
const param = encodeURIComponent('张三&李四');
console.log(param); // %E5%BC%A0%E4%B8%89%26%E6%9D%8E%E5%9B%9B
// encodeURI:编码完整URL(保留 :/?#&= 等结构字符)
const url = encodeURI('https://example.com/search?q=你好 世界');
console.log(url);
// 解码
const decoded = decodeURIComponent(param);
console.log(decoded); // 张三&李四
// RFC 3986 兼容(~ 不编码)
function rfc3986Encode(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase());
}
// URLSearchParams API(现代方式,自动处理编码)
const params = new URLSearchParams({ name: '张三', city: '北京' });
console.log(params.toString());
// name=%E5%BC%A0%E4%B8%89&city=%E5%8C%97%E4%BA%ACfrom urllib.parse import quote, unquote, urlencode, quote_plus
# quote: 类似 encodeURIComponent
encoded = quote('张三&李四')
print(encoded) # %E5%BC%A0%E4%B8%89%26%E6%9D%8E%E5%9B%9B
# RFC 3986 模式(~ 不编码,默认就是)
print(quote('hello~world')) # hello~world
# quote_plus: 空格编码为 + 而非 %20(表单格式)
print(quote_plus('hello world')) # hello+world
# 解码
print(unquote(encoded)) # 张三&李四
# urlencode: 构建查询字符串
params = urlencode({'name': '张三', 'city': '北京'})
print(params)
# name=%E5%BC%A0%E4%B8%89&city=%E5%8C%97%E4%BA%ACimport java.net.URLEncoder;
import java.net.URLDecoder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
public class URLEncodeExample {
public static void main(String[] args) throws Exception {
// URL编码(URLEncoder 是表单格式,空格变+)
String encoded = URLEncoder.encode("张三&李四", "UTF-8");
System.out.println(encoded);
// %E5%BC%A0%E4%B8%89%26%E6%9D%8E%E5%9B%9B
// 解码
String decoded = URLDecoder.decode(encoded, "UTF-8");
System.out.println(decoded); // 张三&李四
// 构建完整URL(URI类自动处理编码)
URI uri = new URI("https", "example.com", "/search", "q=你好世界", null);
System.out.println(uri.toASCIIString());
}
}