通过URL传递base64编码的问题
因为标准的base64编码结果包含"+" 和 “/”,在通过URL传递到服务器时,会被转成空格,造成不能获得正确的解码结果。
所以必须要将 base64 编码后的字符串中的 "+" 替换成 %2B 才能当作 URL 参数进行传递。
在测客户的一个java的应用时,遇到这个问题的。
回想一下自己刚用python做的应用,也用到了base64编码,返回去调查了下。发现python 的base64模块已经解决此问题。
它提供了一个参数,去作这种替换。
Encode a string use Base64.
因为标准的base64编码结果包含"+" 和 “/”,在通过URL传递到服务器时,会被转成空格,造成不能获得正确的解码结果。
所以必须要将 base64 编码后的字符串中的 "+" 替换成 %2B 才能当作 URL 参数进行传递。
在测客户的一个java的应用时,遇到这个问题的。
回想一下自己刚用python做的应用,也用到了base64编码,返回去调查了下。发现python 的base64模块已经解决此问题。
它提供了一个参数,去作这种替换。
b64encode( | s[, altchars]) |
s is the string to encode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies an alternative alphabet for the +
and /
characters. This allows an application to e.g. generate URL or filesystem safe Base64 strings. The default is None
, for which the standard Base64 alphabet is used.