如何在 url 中对 : 进行编码?

How do I encode a : in a url?

我需要发送一个获取请求,其中 url 的最后一部分是 json 值。我已经对以下 {"period":"600s"} 进行编码以在多个不同的站点上使用,但是它们都得出相同的结果,其中 : 未解码。

编码后的url:stickiness=%7B%22period%22%3A%22600s%22%7D.

当我在浏览器中输入它时的结果:

那么我该如何编码 :

%3A :的编码。 :在URI中保留用于指定端口号(例如google.com:443手动指定使用端口443,默认HTTPS端口)。如果要在 URI 中包含 :,它必须是 precent-sign-encoded,这就是 %3A。它无法在 URL 栏中解码,因为它会违反 : 字符的保留用途。

冒号字符不会在浏览器中解码,因为它属于 the reserved characters,在其他地方的 URL 中已经具有明确的含义 - 在这种情况下,将协议与主机名和主机名后的端口分开。

相关标准为RFC 1738,第3页:

Many URL schemes reserve certain characters for a special meaning: their appearance in the scheme-specific part of the URL has a designated semantics. If the character corresponding to an octet is reserved in a scheme, the octet must be encoded. The characters ";", "/", "?", ":", "@", "=" and "&" are the characters which may be reserved for special meaning within a scheme. No other characters may be reserved within a scheme.

Usually a URL has the same interpretation when an octet is represented by a character and when it encoded. However, this is not true for reserved characters: encoding a character reserved for a particular scheme may change the semantics of a URL.

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.