问题描述
当使用Http发送Base64编码的字符串作为标头时,我得到的错误响应为
When sending a Base64 encoded string as header using Http, I am getting error response as
Unexpected char 0x0a at 28 in header value: I99Uy+HjG5PpEhmi8vZgm0W7KDQ=
用法:
String encodedHeader = Base64.encodeToString(value.getBytes(), Base64.DEFAULT);header.put("auth", encodedHeader);
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).
这篇关于在Android中使用OkHttp客户端时,标头值中出现意外的char 0x0a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!