cat haiyangguan.sh
#!/bin/bash
# 获取当前时间戳(毫秒)
current_timestamp=$(($(date +%s%N)/1000000))
# 定义curl命令,使用当前时间戳替换原时间戳
curl_command="curl -H 'Host: ocean.shuziwenlv.com' -H 'content-type: application/json; charset=UTF-8' -H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003133) NetType/WIFI Language/zh_CN' -H 'Referer: https://servicewechat.com/wx49d59947448fc8a7/40/page-frame.html' --data-binary '{\"header\":{\"userNo\":\"\",\"serviceName\":\"CreateOrder\",\"version\":\"v1.0\",\"token\":\"\",\"cacheTime\":0,\"timestamp\":$current_timestamp},\"body\":\"JTdCJTIyZGF0ZSUyMiUzQSUyMjIwMjQtMDYtMDklMjIlMkMlMjJzdHJhdGVneUlkJTIyJTNBJTIyMTAwMDAwMDI1NyUyMiUyQyUyMnByb2plY3RObyUyMiUzQSUyMjEwMDAwMDA2NjYlMjIlMkMlMjJzdWJQcm9qZWN0Tm8lMjIlM0ExMDAwMDAwNjY2JTJDJTIyb3BlbklkJTIyJTNBJTIyb0NhdTU2NGYzTDluYjRIdGlPN1RfOUd6WGExNCUyMiUyQyUyMmFwcE5vJTIyJTNBJTIyMTAlMjIlMkMlMjJ0aWNrZXRMaXN0JTIyJTNBJTVCJTdCJTIyc3R5bGVObyUyMiUzQSUyMjEwMDAwMDEwMDAlMjIlMkMlMjJjb3VudCUyMiUzQTMlMkMlMjJ0b3VyaXN0TGlzdCUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjIlRTUlQkQlQTYlRTYlOTklOTMlRTglOEIlQjklMjIlMkMlMjJtb2JpbGVObyUyMiUzQSUyMjE4NjEwODE1NzA4JTIyJTJDJTIyY2FyZFR5cGUlMjIlM0ElMjIxMCUyMiUyQyUyMmNhcmRObyUyMiUzQSUyMjE1MjMyNjE5ODUwMTE0MzU4OSUyMiUyQyUyMnNlYXRObyUyMiUzQSUyMiUyMiUyQyUyMnNlYXRtYXBObyUyMiUzQSUyMiUyMiUyQyUyMnRpZHglMjIlM0ExJTdEJTJDJTdCJTIybmFtZSUyMiUzQSUyMiVFNSU5MCU5MSVFNCVCOCU5QyUyMiUyQyUyMm1vYmlsZU5vJTIyJTNBJTIyMTg4MTA3NjUxMDglMjIlMkMlMjJjYXJkVHlwZSUyMiUzQSUyMjEwJTIyJTJDJTIyY2FyZE5vJTIyJTNBJTIyNTAwMjMwMTk4NTEwMDI3MTFYJTIyJTJDJTIyc2VhdE5vJTIyJTNBJTIyJTIyJTJDJTIyc2VhdG1hcE5vJTIyJTNBJTIyJTIyJTJDJTIydGlkeCUyMiUzQTIlN0QlMkMlN0IlMjJuYW1lJTIyJTNBJTIyJUU1JTkwJTkxJUU2JUI1JUE5JUU5JUI4JUEzJTIyJTJDJTIybW9iaWxlTm8lMjIlM0ElMjIxMzI2MDE2ODA4NiUyMiUyQyUyMmNhcmRUeXBlJTIyJTNBJTIyMTAlMjIlMkMlMjJjYXJkTm8lMjIlM0ElMjIxMTAxMTIyMDE5MTAyNDQ2MTklMjIlMkMlMjJzZWF0Tm8lMjIlM0ElMjIlMjIlMkMlMjJzZWF0bWFwTm8lMjIlM0ElMjIlMjIlMkMlMjJ0aWR4JTIyJTNBMyU3RCU1RCU3RCU1RCU3RA==\"}' --compressed 'https://ocean.shuziwenlv.com/MiniProgram/ProcessRequest'"
# 执行curl命令
eval $curl_command
#cat run_haiyangguan.sh
^M 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0^M100 1726 100 244 100 1482 829 5039 --:--:-- --:--:-- --:--:-- 5058
{"header":{"status":999,"timecost":19,"msg":"剩余库存不足","serviceName":"CreateOrder","serviceUnavailable":false},"body":""} % Total % Received % Xferd Average Speed Tim
e Time Time Current
Dload Upload Total Spent Left Speed
^M 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0^M100 1482 0 0 100 1482 0 1225 0:00:01 0:00:01 --:--:-- 1225^M100 1798 100
316 100 1482 156 734 0:00:02 0:00:02 --:--:-- 734^M100 1798 100 316 100 1482 156 734 0:00:02 0:00:02 --:--:-- 734
{"header":{"status":200,"timecost":1556,"msg":"操作成功","serviceName":"CreateOrder","serviceUnavailable":false},"body":"JTdiJTIybGF1bmNoJTIyJTNhZmFsc2UlMmMlMjJvcmRlck5vJTIyJTNh
JTIyMjAwMDUzNTg5OCUyMiU3ZA=="} % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
^M 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0^M100 1751 100 269 100 1482 535 2948 --:--:-- --:--:-- --:--:-- 2946^M100 1751 100
269 100 1482 534 2946 --:--:-- --:--:-- --:--:-- 2946
{"header":{"status":999,"timecost":27,"msg":"证件:152**3589请勿重复预定!","serviceName":"CreateOrder","serviceUnavailable":false},"body":""} % Total % Received % Xferd Aver
age Speed Time Time Time Current
Dload Upload Total Spent Left Speed
^M 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0^M100 1751 100 269 100 1482 928 5115 --:--:-- --:--:-- --:--:-- 5128
{"header":{"status":999,"timecost":18,"msg":"证件:152**3589请勿重复预定!","serviceName":"CreateOrder","serviceUnavailable":false},"body":""} % Total % Received % Xferd Aver
age Speed Time Time Time Current
Dload Upload Total Spent Left Speed
^M 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0^M100 1751 100 269 100 1482 547 3015 --:--:-- --:--:-- --:--:-- 3018
{"header":{"status":999,"timecost":17,"msg":"证件:152**3589请勿重复预定!","serviceName":"CreateOrder","serviceUnavailable":false},"body":""} % Total % Received % Xferd Aver
二次升级:
curl -H 'Host: ocean.shuziwenlv.com' -H 'content-type: application/json; charset=UTF-8' -H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003133) NetType/WIFI Language/zh_CN' -H 'Referer: https://servicewechat.com/wx49d59947448fc8a7/40/page-frame.html' --data-binary '{"header":{"userNo":"","serviceName":"CreateOrder","version":"v1.0","token":"","cacheTime":0,"timestamp":1724805497445},"body":"JTdCJTIyZGF0ZSUyMiUzQSUyMjIwMjQtMDktMDElMjIlMkMlMjJzdHJhdGVneUlkJTIyJTNBJTIyMTAwMDAwMDI1NyUyMiUyQyUyMnByb2plY3RObyUyMiUzQSUyMjEwMDAwMDA2NjYlMjIlMkMlMjJzdWJQcm9qZWN0Tm8lMjIlM0ExMDAwMDAwNjY2JTJDJTIyb3BlbklkJTIyJTNBJTIyb0NhdTU2NGYzTDluYjRIdGlPN1RfOUd6WGExNCUyMiUyQyUyMmFwcE5vJTIyJTNBJTIyMTAlMjIlMkMlMjJ0aWNrZXRMaXN0JTIyJTNBJTVCJTdCJTIyc3R5bGVObyUyMiUzQSUyMjEwMDAwMDEwMDAlMjIlMkMlMjJjb3VudCUyMiUzQTQlMkMlMjJ0b3VyaXN0TGlzdCUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjIlRTUlQkQlQTYlRTYlOTklOTMlRTglOEIlQjklMjIlMkMlMjJtb2JpbGVObyUyMiUzQSUyMjE4NjEwODE1NzA4JTIyJTJDJTIyY2FyZFR5cGUlMjIlM0ElMjIxMCUyMiUyQyUyMmNhcmRObyUyMiUzQSUyMjE1MjMyNjE5ODUwMTE0MzU4OSUyMiUyQyUyMnNlYXRObyUyMiUzQSUyMiUyMiUyQyUyMnNlYXRtYXBObyUyMiUzQSUyMiUyMiUyQyUyMnRpZHglMjIlM0ExJTdEJTJDJTdCJTIybmFtZSUyMiUzQSUyMiVFNSU5MCU5MSVFNCVCOCU5QyUyMiUyQyUyMm1vYmlsZU5vJTIyJTNBJTIyMTg4MTA3NjUxMDglMjIlMkMlMjJjYXJkVHlwZSUyMiUzQSUyMjEwJTIyJTJDJTIyY2FyZE5vJTIyJTNBJTIyNTAwMjMwMTk4NTEwMDI3MTFYJTIyJTJDJTIyc2VhdE5vJTIyJTNBJTIyJTIyJTJDJTIyc2VhdG1hcE5vJTIyJTNBJTIyJTIyJTJDJTIydGlkeCUyMiUzQTIlN0QlMkMlN0IlMjJuYW1lJTIyJTNBJTIyJUU1JTkwJTkxJUU2JUI1JUE5JUU5JUI4JUEzJTIyJTJDJTIybW9iaWxlTm8lMjIlM0ElMjIxMzI2MDE2ODA4NiUyMiUyQyUyMmNhcmRUeXBlJTIyJTNBJTIyMTAlMjIlMkMlMjJjYXJkTm8lMjIlM0ElMjIxMTAxMTIyMDE5MTAyNDQ2MTklMjIlMkMlMjJzZWF0Tm8lMjIlM0ElMjIlMjIlMkMlMjJzZWF0bWFwTm8lMjIlM0ElMjIlMjIlMkMlMjJ0aWR4JTIyJTNBMyU3RCUyQyU3QiUyMm5hbWUlMjIlM0ElMjIlRTUlOTAlOTElRTUlQUUlODclMjIlMkMlMjJtb2JpbGVObyUyMiUzQSUyMjE2NjIzMTYwOTAyJTIyJTJDJTIyY2FyZFR5cGUlMjIlM0ElMjIxMCUyMiUyQyUyMmNhcmRObyUyMiUzQSUyMjUwMDIzMDIwMDQwOTAyNzExNSUyMiUyQyUyMnNlYXRObyUyMiUzQSUyMiUyMiUyQyUyMnNlYXRtYXBObyUyMiUzQSUyMiUyMiUyQyUyMnRpZHglMjIlM0E0JTdEJTVEJTdEJTVEJTdECg=="}' --compressed https://ocean.shuziwenlv.com/MiniProgram/ProcessRequest
{"header":{"status":301,"timecost":0,"msg":"请求header传入的方法名称不正确","serviceName":"CreateOrder","serviceUnavailable":false},"body":""}
cat haiyangguan.sh
#!/bin/bash
# 获取当前时间戳(毫秒)
current_timestamp=$(($(date +%s%N)/1000000))
# 定义curl命令,使用当前时间戳替换原时间戳
curl_command="curl -H 'Host: ocean.shuziwenlv.com' -H 'content-type: application/json; charset=UTF-8' -H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605
.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003133) NetType/WIFI Language/zh_CN' -H 'Referer: https://servicewechat.com/wx49d59947448fc8a7/40/page-frame.html' --data-bi
nary '{\"header\":{\"userNo\":\"\",\"serviceName\":\"CreateOrder\",\"version\":\"v1.0\",\"token\":\"\",\"cacheTime\":0,\"timestamp\":$current_timestamp},\"body\":\"JTdCJTIyZGF0ZSUyMiUzQSUyMj
IwMjQtMDktMDElMjIlMkMlMjJzdHJhdGVneUlkJTIyJTNBJTIyMTAwMDAwMDI1NyUyMiUyQyUyMnByb2plY3RObyUyMiUzQSUyMjEwMDAwMDA2NjYlMjIlMkMlMjJzdWJQcm9qZWN0Tm8lMjIlM0ExMDAwMDAwNjY2JTJDJTIyb3BlbklkJTIyJTNBJTIy
b0NhdTU2NGYzTDluYjRIdGlPN1RfOUd6WGExNCUyMiUyQyUyMmFwcE5vJTIyJTNBJTIyMTAlMjIlMkMlMjJ0aWNrZXRMaXN0JTIyJTNBJTVCJTdCJTIyc3R5bGVObyUyMiUzQSUyMjEwMDAwMDEwMDAlMjIlMkMlMjJjb3VudCUyMiUzQTQlMkMlMjJ0b3
VyaXN0TGlzdCUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjIlRTUlQkQlQTYlRTYlOTklOTMlRTglOEIlQjklMjIlMkMlMjJtb2JpbGVObyUyMiUzQSUyMjE4NjEwODE1NzA4JTIyJTJDJTIyY2FyZFR5cGUlMjIlM0ElMjIxMCUyMiUyQyUyMmNhcmRO
byUyMiUzQSUyMjE1MjMyNjE5ODUwMTE0MzU4OSUyMiUyQyUyMnNlYXRObyUyMiUzQSUyMiUyMiUyQyUyMnNlYXRtYXBObyUyMiUzQSUyMiUyMiUyQyUyMnRpZHglMjIlM0ExJTdEJTJDJTdCJTIybmFtZSUyMiUzQSUyMiVFNSU5MCU5MSVFNCVCOCU5Qy
UyMiUyQyUyMm1vYmlsZU5vJTIyJTNBJTIyMTg4MTA3NjUxMDglMjIlMkMlMjJjYXJkVHlwZSUyMiUzQSUyMjEwJTIyJTJDJTIyY2FyZE5vJTIyJTNBJTIyNTAwMjMwMTk4NTEwMDI3MTFYJTIyJTJDJTIyc2VhdE5vJTIyJTNBJTIyJTIyJTJDJTIyc2Vh
dG1hcE5vJTIyJTNBJTIyJTIyJTJDJTIydGlkeCUyMiUzQTIlN0QlMkMlN0IlMjJuYW1lJTIyJTNBJTIyJUU1JTkwJTkxJUU2JUI1JUE5JUU5JUI4JUEzJTIyJTJDJTIybW9iaWxlTm8lMjIlM0ElMjIxMzI2MDE2ODA4NiUyMiUyQyUyMmNhcmRUeXBlJT
IyJTNBJTIyMTAlMjIlMkMlMjJjYXJkTm8lMjIlM0ElMjIxMTAxMTIyMDE5MTAyNDQ2MTklMjIlMkMlMjJzZWF0Tm8lMjIlM0ElMjIlMjIlMkMlMjJzZWF0bWFwTm8lMjIlM0ElMjIlMjIlMkMlMjJ0aWR4JTIyJTNBMyU3RCUyQyU3QiUyMm5hbWUlMjIl
M0ElMjIlRTUlOTAlOTElRTUlQUUlODclMjIlMkMlMjJtb2JpbGVObyUyMiUzQSUyMjE2NjIzMTYwOTAyJTIyJTJDJTIyY2FyZFR5cGUlMjIlM0ElMjIxMCUyMiUyQyUyMmNhcmRObyUyMiUzQSUyMjUwMDIzMDIwMDQwOTAyNzExNSUyMiUyQyUyMnNlYX
RObyUyMiUzQSUyMiUyMiUyQyUyMnNlYXRtYXBObyUyMiUzQSUyMiUyMiUyQyUyMnRpZHglMjIlM0E0JTdEJTVEJTdEJTVEJTdECg==\"}' --compressed 'https://ocean.shuziwenlv.com/MiniProgram/ProcessRequest'"
# 执行curl命令
eval $curl_command
URLEncode后再Base64编码传输到服务器端的研究:
上面将URL的内容还成成可读的内容,专业术语叫什么来着。
同样,反过来将可读内容urlencode成传输内容的术语叫啥?
将URL编码的内容还原成可读内容的过程称为URL解码(URL Decoding)。相反,将可读内容转换为URL编码格式的过程称为URL编码(URL Encoding)。
以下是使用PHP和Go语言进行URL编码(URL Encoding)和解码(URL Decoding)的示例代码。
### 1. PHP
#### URL解码(URL Decoding)
```php
<?php
// URL编码后的JSON
$urlEncoded = "%7B%22date%22%3A%222024-09-01%22%2C%22strategyId%22%3A%221000000257%22%2C%22projectNo%22%3A%221000000666%22%2C%22subProjectNo%22%3A1000000666%2C%22openId%22%3A%22oCau564f3L9nb4HtiO7T_9GzXa14%22%2C%22appNo%22%3A%2210%22%2C%22ticketList%22%3A%5B%7B%22styleNo%22%3A%221000001000%22%2C%22count%22%3A4%2C%22touristList%22%3A%5B%7B%22name%22%3A%22%E5%BD%A6%E6%99%93%E8%8B%B9%22%2C%22mobileNo%22%3A%2218610815708%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22152326198501143589%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A1%7D%2C%7B%22name%22%3A%22%E5%90%91%E4%B8%9C%22%2C%22mobileNo%22%3A%2218810765108%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%2250023019851002711X%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A2%7D%2C%7B%22name%22%3A%22%E5%90%91%E6%B5%A9%E9%B8%A3%22%2C%22mobileNo%22%3A%2213260168086%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22110112201910244619%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A3%7D%2C%7B%22name%22%3A%22%E5%90%91%E5%AE%87%22%2C%22mobileNo%22%3A%2216623160902%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22500230200409027115%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A4%7D%5D%7D%5D%7D";
// 进行URL解码
$decoded = urldecode($urlEncoded);
// 打印解码后的JSON
echo $decoded;
?>
```
#### URL编码(URL Encoding)
```php
<?php
// 明文JSON内容
$json = '{
"date": "2024-09-01",
"strategyId": "1000000257",
"projectNo": "1000000666",
"subProjectNo": 1000000666,
"openId": "oCau564f3L9nb4HtiO7T_9GzXa14",
"appNo": "10",
"ticketList": [
{
"styleNo": "1000001000",
"count": 4,
"touristList": [
{
"name": "彦晓苹",
"mobileNo": "18610815708",
"cardType": "10",
"cardNo": "152326198501143589",
"seatNo": "",
"seatmapNo": "",
"tidx": 1
},
{
"name": "向东",
"mobileNo": "18810765108",
"cardType": "10",
"cardNo": "50023019851002711X",
"seatNo": "",
"seatmapNo": "",
"tidx": 2
},
{
"name": "向浩鸣",
"mobileNo": "13260168086",
"cardType": "10",
"cardNo": "110112201910244619",
"seatNo": "",
"seatmapNo": "",
"tidx": 3
},
{
"name": "向宇",
"mobileNo": "16623160902",
"cardType": "10",
"cardNo": "500230200409027115",
"seatNo": "",
"seatmapNo": "",
"tidx": 4
}
]
}
]
}';
// 进行URL编码
$encoded = urlencode($json);
// 打印编码后的URL格式内容
echo $encoded;
?>
```
### 2. Go
#### URL解码(URL Decoding)
```go
package main
import (
"fmt"
"net/url"
)
func main() {
// URL编码后的JSON
urlEncoded := "%7B%22date%22%3A%222024-09-01%22%2C%22strategyId%22%3A%221000000257%22%2C%22projectNo%22%3A%221000000666%22%2C%22subProjectNo%22%3A1000000666%2C%22openId%22%3A%22oCau564f3L9nb4HtiO7T_9GzXa14%22%2C%22appNo%22%3A%2210%22%2C%22ticketList%22%3A%5B%7B%22styleNo%22%3A%221000001000%22%2C%22count%22%3A4%2C%22touristList%22%3A%5B%7B%22name%22%3A%22%E5%BD%A6%E6%99%93%E8%8B%B9%22%2C%22mobileNo%22%3A%2218610815708%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22152326198501143589%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A1%7D%2C%7B%22name%22%3A%22%E5%90%91%E4%B8%9C%22%2C%22mobileNo%22%3A%2218810765108%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%2250023019851002711X%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A2%7D%2C%7B%22name%22%3A%22%E5%90%91%E6%B5%A9%E9%B8%A3%22%2C%22mobileNo%22%3A%2213260168086%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22110112201910244619%22%2C%22seatNo%22%3A%22%22%2C%22seatmapNo%22%3A%22%22%2C%22tidx%22%3A3%7D%2C%7B%22name%22%3A%22%E5%90%91%E5%AE%87%22%2C%22mobileNo%22%3A%2216623160902%22%2C%22cardType%22%3A%2210%22%2C%22cardNo%22%3A%22500230200409027115%22%2C%22
用PHP和GO语言,分别将上面的JSON进行解码和编码,学习一下,谢谢。
问题:
为何要进行URL编码?以及它对哪些字符作了转化,转化的目的是什么?
回答:
URL编码(URL Encoding)的主要目的是确保在URL中传输数据时,字符不会被错误地解释或丢失。URL中的某些字符具有特殊的意义(如`?`, `&`, `/`, `=`等),它们被用来分隔URL的不同部分(例如查询参数),因此如果这些字符出现在数据中(如查询字符串、路径参数或表单数据),它们可能会导致URL解析错误。URL编码通过将这些字符转换为特定的编码形式来避免此类问题。
### 为什么进行URL编码?
1. **避免歧义**:URL中的特殊字符(如`?`, `&`, `/`等)可能会与实际数据中的字符混淆,导致URL解析错误。URL编码能够将这些特殊字符转换为不具有特殊意义的形式,以避免歧义。
2. **确保兼容性**:某些字符在URL中是非法的(如空格、引号、尖括号等),或者某些字符在不同的浏览器和服务器之间的处理可能不同。通过URL编码,可以确保这些字符在所有情况下都能正确传输和解释。
3. **编码非ASCII字符**:URL通常只能使用ASCII字符集,因此需要将非ASCII字符(如中文、日文等)编码为ASCII字符,以确保它们能在URL中被正确传输。
### URL编码对哪些字符进行了转化?
在URL编码中,某些字符会被转换为以`%`开头的三字符表示形式,其中后两字符是该字符的ASCII值的十六进制表示。以下是常见的字符及其URL编码后的形式:
- **空格(Space)**: `%20`
- **双引号(")**: `%22`
- **#号**: `%23`
- **$符号**: `%24`
- **%符号**: `%25`
- **&符号**: `%26`
- **'符号**: `%27`
- **(符号**: `%28`
- **)符号**: `%29`
- **+号**: `%2B`
- **,符号**: `%2C`
- **/符号**: `%2F`
- **:符号**: `%3A`
- **;符号**: `%3B`
- **<符号**: `%3C`
- **=符号**: `%3D`
- **>符号**: `%3E`
- **?符号**: `%3F`
- **@符号**: `%40`
- **[符号**: `%5B`
- **\符号**: `%5C`
- **]符号**: `%5D`
- **`符号**: `%60`
- **{符号**: `%7B`
- **|符号**: `%7C`
- **}符号**: `%7D`
### 转化的目的是什么?
1. **安全传输**:防止在URL中传输数据时,因特殊字符导致数据的误解析或错误解释。
2. **合法性**:保证URL符合标准规范,避免因非法字符导致的解析问题。
3. **一致性**:确保不同系统、浏览器、服务器对URL的处理和理解是一致的。
URL编码是网络通信中不可或缺的一部分,确保了数据在不同环境之间的安全、可靠传输。
问题:UrlEncode主要是放在浏览器的GET参数中传输吧,POST中就不需要进行UrlEncode吧? 为什么经过了urlencode的数据放在curl中的--data-binary还要再进行一次base64转码呢?
答案:
URL编码主要用于确保数据在URL中传输时不被错误解释。对于GET请求,参数通常直接附在URL后面,因此必须进行URL编码以处理特殊字符。但对于POST请求,数据通常在请求体中传输,这种情况下,是否需要URL编码取决于数据格式和传输方式。
### 关于URL编码在POST请求中的使用:
1. **表单提交(`application/x-www-form-urlencoded`)**:
- 默认情况下,表单数据会自动使用`application/x-www-form-urlencoded`的MIME类型,这意味着表单字段的值将被URL编码。
- 例如,`name=John Doe&age=25`会变成`name=John%20Doe&age=25`。
2. **JSON或其他数据格式(`application/json`)**:
- 如果POST请求以JSON或其他格式发送数据(如`application/json`),通常不需要URL编码,因为数据是以原始格式直接放在请求体中。
- 例如,JSON数据`{"name": "John Doe", "age": 25}`不会被编码,而是直接作为请求体的一部分发送。
### 为什么在`curl`中使用`--data-binary`的同时还要进行Base64编码?
Base64编码是一种将二进制数据转换为ASCII字符的编码方式。以下是一些场景中使用Base64编码的原因:
1. **处理二进制数据**:
- `--data-binary`在`curl`中用于直接传输原始数据,不进行任何处理。对于某些API或服务器而言,如果数据中包含不可见字符或特殊字符(例如图像、加密数据、二进制文件),可能会导致解析问题。因此,使用Base64编码可以确保这些数据在传输过程中不会被误解或损坏。
2. **兼容性**:
- 某些服务器或应用可能要求或更容易处理Base64编码的数据。Base64编码将数据转换为只包含字母、数字及少量符号的ASCII字符,从而避免了特殊字符导致的解析问题。
3. **安全性**:
- 在某些情况下,数据经过URL编码后仍然可能包含对服务器或API具有特殊意义的字符。这些字符可能会触发某些意外行为或安全风险。通过额外的Base64编码,可以增加数据的可靠性和安全性,防止这些问题的发生。
### 综合考虑:
- 在GET请求中,URL编码是必须的。
- 在POST请求中,URL编码取决于内容类型和数据格式。
- Base64编码是为了解决特殊字符、二进制数据传输问题,以及提高数据的安全性和兼容性。
因此,数据即使经过URL编码,在传输中可能仍然需要Base64编码以确保数据的完整性和安全性。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/12431/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2024-8-28 09:22
评论列表