首页 > Web开发 > 详细

URL编码

时间:2019-06-07 16:30:14      阅读:82      评论:0      收藏:0      [点我收藏+]

在web服务开发中,URL编码问题是绕不过的,本文简单总结一下。

由于一些特殊符号的存在,为了避免歧义,需要将这些特殊符号经过编码之后传递。
URL编码中最常见的就是加号(+)和空格( )了。

我们使用如下代码部署一个web服务

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    if r.Method == "GET" {
        for k, v := range r.Form {
            fmt.Printf("key:%s,value:%v\n", k, v)
        }
    } else {
        for k, v := range r.PostForm {
            fmt.Printf("key:%s,value:%v\n", k, v)
        }
    }
}

通过postman测试,得到的结论如下

GET请求

请求 实际请求 响应
a=hello world a=hello%20world a=hello world
a=hello+world a=hello+world a=hello world
a=hello%2Bworld a=hello%2Bworld a=hello+world

POST请求(Content-Type: application/x-www-form-urlencoded)

请求 实际请求 响应
a=hello world a=hello%20world a=hello world
a=hello+world a=hello%2Bworld a=hello+world
a=hello%2Bworld a=hello%252Bworld a=hello%2Bworld

参考资料

URL编码 by 阮一峰

https://en.wikipedia.org/wiki/Percent-encoding

https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20

URL编码

原文:https://www.cnblogs.com/wjz2047/p/10988292.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!