本文目录导读:

我来为您介绍如何编写URL编码解码脚本,提供多种编程语言的实现方法。
Python 实现
Python 有内置的 urllib.parse 模块:
from urllib.parse import quote, unquote, urlencode, parse_qs
# URL编码
def url_encode(text):
"""对字符串进行URL编码"""
return quote(text, safe='')
# URL解码
def url_decode(text):
"""对URL编码的字符串进行解码"""
return unquote(text)
# 示例
original_text = "你好世界 Hello World! @#$%"
encoded = url_encode(original_text)
decoded = url_decode(encoded)
print(f"原始文本: {original_text}")
print(f"编码后: {encoded}")
print(f"解码后: {decoded}")
# 编码URL参数
params = {'name': '张三', 'age': 25, 'city': '北京'}
encoded_params = urlencode(params)
print(f"编码参数: {encoded_params}")
# 解码URL参数
decoded_params = parse_qs(encoded_params)
print(f"解码参数: {decoded_params}")
JavaScript (浏览器端)
// URL编码
function urlEncode(text) {
return encodeURIComponent(text);
}
// URL解码
function urlDecode(encodedText) {
return decodeURIComponent(encodedText);
}
// 示例
const originalText = "你好世界 Hello World! @#$%";
const encoded = urlEncode(originalText);
const decoded = urlDecode(encoded);
console.log(`原始文本: ${originalText}`);
console.log(`编码后: ${encoded}`);
console.log(`解码后: ${decoded}`);
// 编码完整URL(保留特殊字符)
function encodeFullURL(url) {
return encodeURI(url);
}
// URL参数对象编码
function encodeParams(params) {
return Object.entries(params)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}
// 示例
const params = {name: '张三', age: 25, city: '北京'};
console.log(`编码参数: ${encodeParams(params)}`);
Node.js 实现
const querystring = require('querystring');
// URL编码
function urlEncode(text) {
return querystring.escape(text);
}
// URL解码
function urlDecode(encodedText) {
return querystring.unescape(encodedText);
}
// 使用内置模块
function advancedURLEncode(text) {
return encodeURIComponent(text);
}
function advancedURLDecode(encodedText) {
return decodeURIComponent(encodedText);
}
// 示例
const originalText = "你好世界 Hello World! @#$%";
console.log(`原始文本: ${originalText}`);
console.log(`编码后: ${urlEncode(originalText)}`);
console.log(`解码后: ${urlDecode(urlEncode(originalText))}`);
// URL参数编码
const params = {name: '张三', age: 25, city: '北京'};
const encodedParams = querystring.stringify(params);
console.log(`编码参数: ${encodedParams}`);
// URL参数解码
const decodedParams = querystring.parse(encodedParams);
console.log(`解码参数:`, decodedParams);
Java 实现
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
public class URLCodec {
// URL编码
public static String urlEncode(String text) {
try {
return URLEncoder.encode(text, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
// URL解码
public static String urlDecode(String encodedText) {
try {
return URLDecoder.decode(encodedText, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
// 编码URL参数
public static String encodeParams(Map<String, String> params) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(urlEncode(entry.getKey()));
sb.append("=");
sb.append(urlEncode(entry.getValue()));
}
return sb.toString();
}
public static void main(String[] args) {
String originalText = "你好世界 Hello World! @#$%";
String encoded = urlEncode(originalText);
String decoded = urlDecode(encoded);
System.out.println("原始文本: " + originalText);
System.out.println("编码后: " + encoded);
System.out.println("解码后: " + decoded);
// 编码参数
Map<String, String> params = new HashMap<>();
params.put("name", "张三");
params.put("age", "25");
params.put("city", "北京");
System.out.println("编码参数: " + encodeParams(params));
}
}
PHP 实现
<?php
// URL编码
function urlEncode($text) {
return urlencode($text);
}
// URL解码
function urlDecode($encodedText) {
return urldecode($encodedText);
}
// 示例
$originalText = "你好世界 Hello World! @#$%";
$encoded = urlEncode($originalText);
$decoded = urlDecode($encoded);
echo "原始文本: $originalText\n";
echo "编码后: $encoded\n";
echo "解码后: $decoded\n";
// URL参数编码
$params = ['name' => '张三', 'age' => 25, 'city' => '北京'];
$encodedParams = http_build_query($params);
echo "编码参数: $encodedParams\n";
// URL参数解码
parse_str($encodedParams, $decodedParams);
print_r($decodedParams);
?>
命令行工具 (Linux/Unix)
#!/bin/bash
# URL编码函数
url_encode() {
local string="$1"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
# URL解码函数
url_decode() {
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# 示例
original_text="你好世界 Hello World! @#$%"
encoded=$(url_encode "$original_text")
decoded=$(url_decode "$encoded")
echo "原始文本: $original_text"
echo "编码后: $encoded"
echo "解码后: $decoded"
使用建议
- Python: 适合数据处理和自动化任务
- JavaScript: 适合Web开发
- Java: 适合企业级应用
- PHP: 适合Web服务端
- Bash: 适合Linux环境自动化
选择哪种语言取决于您的具体应用场景和开发环境。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播