在 Android 中使用 OkHttp 客户端时,标头值中出现意外的 char 0x0a

2022-01-21 00:00:00 base64 android java okhttp retrofit

使用 Http 发送 Base64 编码字符串作为标头时,我收到错误响应

When sending a Base64 encoded string as header using Http, I am getting error response as

标头值中 28 处的意外字符 0x0a:I99Uy+HjG5PpEhmi8vZgm0W7KDQ=

用法:

String encodedHeader = Base64.encodeToString(value.getBytes(), Base64.DEFAULT);header.put("auth", encodedHeader);

推荐答案

0x0a 是一个换行符,在标题中是被禁止的.解决方案是确保在将编码值作为标头发送之前删除这些字符.

0x0a is a newline character which is forbidden in a header. Solution would be to make sure that these characters are stripped off before sending the encoded value as header.

Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);这样可以避免使用特定于平台的换行符换行.

Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); this avoids wrapping with a platform specific newline character(s).

相关文章