# [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#url-encoding)Url Encoding
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#general-case)General Case
来自RFC 3986 (2005)
> 2.2 保留字符
>
> URI包括由“保留”集中的字符分隔的组件和子组件。 这些字符称为“保留”,因为它们可能(或可能不)通过通用语法,每种特定于方案的语法或URI的解除引用算法的特定于实现的语法定义为分隔符。 如果URI组件的数据与保留字符作为分隔符的目的冲突,则冲突数据必须在形成URI之前进行百分比编码。
>
> ~~~
> reserved = gen-delims / sub-delims
> gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
> sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
>
> ~~~
>
> 2.3 未保留的字符
>
> `unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"`
>
> 2.4 何时进行编码或解码
>
> 由于百分比(“%”)字符用作百分比编码八位字节的指示符,因此必须将该八位字节的百分比编码为“%25”,以用作URI中的数据
基于URI语法组件指定以下子集:
~~~
以下是两个示例URI及其组成部分:
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
| _____________________|__
/ \ / \
urn:example:animal:ferret:nose
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
authority = [ userinfo "@" ] host [ ":" port ]
path = ... pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
query = *( pchar / "/" / "?" )
fragment = *( pchar / "/" / "?" )
~~~
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#query-string)Query String
虽然RFC 3986定义了查询字符串中允许的字符,但数据在表单url-encoding的查询字符串中进行编码。 该编码算法由W3C`application / x-www-form-urlencoded`编码算法和RFC1738(1994)定义。
> 对于条目名称和值中无法使用所选字符编码表示的每个字符,请将字符替换为由U + 0026 AMPERSAND字符(&),“#”(U + 0023)字符组成的字符串,一个或 更多的ASCII数字代表十进制中字符的Unicode代码点,最后是“;” (U + 003B)字符。
>
> 对于条目名称和值中的每个字节,应用以下列表中的相应子子步骤:
>
> 如果字节为0x20(如果解释为ASCII,则为U + 0020 SPACE)将字节替换为单个0x2B字节(如果解释为ASCII,则为“+”(U + 002B)字符)。 如果字节在0x2A,0x2D,0x2E,0x30至0x39,0x41至0x5A,0x5F,0x61至0x7A范围内,则保持字节不变。
>
> 否则,让`s`是一个由U + 0025 PERCENT SIGN字符(%)组成的字符串,后跟大写的ASCII十六进制数字,表示所讨论字节的十六进制值(必要时填零)。
>
> 将字符串`s`编码为US-ASCII,以便它现在是一个字节字符串。
>
> 用`s`中的字节替换正在处理的名称或值中的字节,保留它们的相对顺序。
在空间中使用“+”是一个相当有争议的问题,有些情况下它被削弱为“+”或“%20”。.
另外,OAuth 1.0定义了所需的参数编码,用于准备RFC 5849 3.6中定义的签名和授权标头:
* [RFC3986]第2.3节定义的非保留字符集中的字符
`(ALPHA, DIGIT, "-", ".", "_", "~")` 绝不能编码。
* 必须编码所有其他字符。
* 用于表示编码的两个十六进制字符
字符必须是大写的。
它包括以下注释:
> 此方法不同于“application / x-www-form-urlencoded”内容类型使用的编码方案(例如,它将空格字符编码为“%20”而不使用“+”字符)。
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#cookies)Cookies
cookie实现似乎存在差异,规格范围从原始(cookie \ _spec)到最新的RFC 6265。
**cookie\_spec**
`NAME=VALUE` “此字符串是一系列字符,不包括分号,逗号和空格。如果需要在名称或值中放置此类数据,建议使用某种编码方法,如URL样式%XX编码,但不编码 定义或要求。“
**RFC 6265**
~~~
cookie-pair = cookie-name "=" cookie-value
cookie-name = token
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
; US-ASCII characters excluding CTLs,
; whitespace DQUOTE, comma, semicolon,
; and backslash
(from RFC 2616)
token = 1*<any CHAR except CTLs or separators>
separators = "(" | ")" | "<" | ">" | "@"
| "," | ";" | ":" | "\" | <">
| "/" | "[" | "]" | "?" | "="
| "{" | "}" | SP | HT
~~~
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#usage)Usage
VBA-Web中`UrlEncode`的当前用法:
* OAuth1 - Encode signature and header
* `ConvertToUrlEncoded` - Encode key and value (used by `WebFormat.UrlEncoded` and to encode `QuerystringParams`)
* UrlSegments - Segment value is encoded during replacement
* Note: Currently cookies are not encoded for requests (should be in the future)
`UrlDecode`:
* `ParseUrlEncoded` (used by `WebFormat.UrlEncoded` and in `FacebookAuthenticator`, `TodoistAuthenticator`)
* Cookies - (`PlusAsSpace:=False`)
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#implementation)Implementation
The updated `UrlEncode` and `UrlDecode` will have the following "modes":
* `strict`: `unreserved`\-only (shared with OAuth 1.0) and is the default
* `form-urlencoded`: used only for `WebFormat.UrlEncoded` and uses `+` for space
* `querystring`: subset of `strict` and `form-urlencoded` = `ALPHA / DIGIT / "-" / "." / "_"`
* `cookie`
* `path`
For query strings, `form-urlencoded` will be used for `WebFormat.UrlEncoded`, otherwise `querystring` will be used. Also, For `UrlDecode`, `querystring` converts `+` to `space` for backwards compatibility.
~~~
strict = ALPHA / DIGIT / "-" / "." / "_" / "~"
formurlencoded = ALPHA / DIGIT / "-" / "." / "_" / "*", (space) => "+"
query = ALPHA / DIGIT / "-" / "." / "_"
cookie = strict / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "^" / "`" / "|"
path = strict / "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" / ":" / "@"
~~~
~~~vbnet
Public Enum UrlEncodingMode
StrictUrlEncoding
FormUrlEncoding
QueryUrlEncoding
CookieUrlEncoding
PathUrlEncoding
End Enum
Public Function UrlEncode(Text As String, ..., Optional EncodingMode As UrlEncodingMode = StrictEncoding)
' ...
End Function
Public Function UrlDecode(Encoded As String, ..., Optional EncodingMode As UrlEncodingMode = StrictEncoding)
End Function
~~~
## [](https://github.com/VBA-tools/VBA-Web/wiki/Url-Encoding#references)References
* RFC 3986, [https://tools.ietf.org/html/rfc3986](https://tools.ietf.org/html/rfc3986)
* Percent Encoding, [https://en.wikipedia.org/wiki/Percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding)
* form-urlencoded encoding algorithm, [https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm](https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm)
* RFC 1738, [https://tools.ietf.org/html/rfc1738](https://tools.ietf.org/html/rfc1738)
* RFC 5849 - OAuth 1.0, [http://tools.ietf.org/html/rfc5849#section-3.6](http://tools.ietf.org/html/rfc5849#section-3.6)
* cookie\_spec, [https://curl.haxx.se/rfc/cookie\_spec.html](https://curl.haxx.se/rfc/cookie_spec.html)
* RFC 6265, [https://tools.ietf.org/html/rfc6265](https://tools.ietf.org/html/rfc6265)
* RFC 2616, [https://tools.ietf.org/html/rfc2616#section-2.2](https://tools.ietf.org/html/rfc2616#section-2.2)
- README
- 指南
- 概述
- GET Request
- WebRequest
- 属性
- Resource
- Method
- Body
- Format
- RequestFormat
- ResponseFormat
- CustomRequestFormat
- CustomResponseFormat
- ContentType
- Accept
- ContentLength
- FormattedResource
- Cookies
- Headers
- QuerystringParams
- UrlSegments
- 方法
- AddHeader
- SetHeader
- AddUrlSegment
- AddQuerystringParam
- AddCookie
- AddBodyParameter
- CreateFromOptions
- WebClient
- 属性
- BaseUrl
- Authenticator
- TimeoutMs
- ProxyServer
- ProxyBypassList
- ProxyUsername
- ProxyPassword
- EnableAutoProxy
- Insecure
- FollowRedirects
- 方法
- Execute
- GetJson
- PostJson
- SetProxy
- GetFullUrl
- WebResponse
- 属性
- StatusCode
- StatusDescription
- Content
- Data
- Body
- Headers
- Cookies
- 方法
- Update
- WebHelpers
- 属性
- WebStatusCode
- WebMethod
- WebFormat
- UrlEncodingMode
- EnableLogging
- 方法
- LogDebug
- LogWarning
- LogError
- LogRequest
- LogResponse
- Obfuscate
- ParseJson
- ConvertToJson
- ParseUrlEncoded
- ConvertToUrlEncoded
- ParseXml
- ConvertToXml
- ParseByFormat
- ConvertToFormat
- UrlEncode
- UrlDecode
- Base64Encode
- Base64Decode
- RegisterConverter
- JoinUrl
- UrlParts
- CloneDictionary
- CloneCollection
- CreateKeyValue
- FindInKeyValues
- AddOrReplaceInKeyValues
- FormatToMediaType
- MethodToName
- HMACSHA1
- HMACSHA256
- MD5
- CreateNonce
- IWebAuthenticator
- 方法
- BeforeExecute
- AfterExecute
- PrepareHttp
- PrepareCurl
- WebAsyncWrapper
- 属性
- Client
- 方法
- ExecuteAsync
- 范例
- Salesforce网站
- Google APIs
- Todoist API
- 其他主题
- 调试
- 授权
- 实现自己的IWebAuthenticator
- Url编码