# [](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編碼