go URL编码 base64 编码 记录
URL 编码/解码
RFC3986 协议对 URL 的编解码问题做出了详细的建议,指出了哪些字符需要被编码才不会引起 URL 语义的转变,以及对为什么这些字符需要编码做出了相应的解释。
RFC3986 协议规定 URL 只允许包含以下四种字符:
1、英文字母(a-zA-Z)
2、数字(0-9)
3、-_.~ 4个特殊字符
4、所有保留字符,RFC3986 中指定了以下字符为保留字符(英文字符): ! * ' ( ) ; : @ & = + $ , / ? # [ ]
JavaScript 的 decodeURIComponent 方法进行解码,encodeURI 和 encodeURIComponent 方法进行编码。
encodeURI 方法不会对ASCII字母、数字、~!@#$&*()=:/,;?+' 编码。 go中实现为:构造 url.URL 并调用 .String();
parsedURL, err := url.Parse(fullURLStr) if err != nil { log.Fatal(err) } //调用 .String() 方法会得到一个被正确编码的完整 URL 字符串 encodedFullURL := parsedURL.String()
encodeURIComponent 方法不会对ASCII字母、数字、~!*()' 编码。 相当于 go 中的url.QueryEscape(str)
-
当你不确定时,先思考你要编码的内容是放在
?的后面还是前面。 -
放在
?后面(作为参数),用url.QueryEscape。 -
放在
?前面(作为路径),用url.PathEscape go 中// 编码单个路径段,会将 `/` 也进行编码
// 这是一个需要作为URL参数传递的复杂字符串 componentStr := "go/&language=中文?q=1" // 使用 url.QueryEscape 对其进行编码 // 这会编码 '/', '&', '=', '?' 等所有特殊字符 encodedComponent := url.QueryEscape(componentStr) fmt.Printf("原始字符串: %s\n", componentStr) fmt.Printf("Go (url.QueryEscape) 编码后: %s\n", encodedComponent) // JavaScript encodeURIComponent('go/&language=中文?q=1') 的结果也是: // go%2F%26language%3D%E4%B8%AD%E6%96%87%3Fq%3D1
encodeURIComponent 比 encodeURI 编码的范围大。
因此当你需要编码整个 URL,就用 encodeURI。
如果只需要编码 URL 中的参数时,就使用 encodeURIComponent。
func UtilEncodeURL(input string, mode string) string { var reserveChars string switch mode { case "escape": reserveChars = "*/@+-._0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" case "encodeURI": reserveChars = "!#$&'()*+,/:;=?@-._~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" case "encodeURIComponent": reserveChars = "!'()*-._~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" default: return input } var result strings.Builder for _, ch := range input { if strings.ContainsRune(reserveChars, ch) { result.WriteRune(ch) } else { result.WriteString(fmt.Sprintf("%%%X", ch)) } } return result.String() }
BASE64:
base64.StdEncoding 标准 Base64 编码(末尾可能有 = 补齐) base64.URLEncoding URL 安全编码(使用 - 和 _ 替代 + 和 /) base64.RawStdEncoding 标准编码(无 = 补齐) base64.RawURLEncoding URL 编码(无 = 补齐)
http代理服务器(3-4-7层代理)-网络事件库公共组件、内核kernel驱动 摄像头驱动 tcpip网络协议栈、netfilter、bridge 好像看过!!!!
但行好事 莫问前程
--身高体重180的胖子

浙公网安备 33010602011771号