OkHttp IllegalArgumentException 异常解决方法

在使用 okhttp 的时候,head 的一些项是中文,导致网络请求失败,错误类似下面的

java.lang.IllegalArgumentException: Unexpected char ...

找了一圈发现是 okhttp 对 head 的编码做了验证

Header values are (technically) required to be ISO-8859-1 but in practice only ASCII really works and OkHttp validates such (the exception has nothing to do with Retrofit). That string is a custom user agent and you'll need to restrict its contents to the ASCII character set when creating it.

具体的代码 ( 在 okhttp3 库里面的 okhttp3.Headers.java ) 如下:

private void checkNameAndValue(String name, String value) {
    if (name == null) throw new NullPointerException("name == null");
    if (name.isEmpty()) throw new IllegalArgumentException("name is empty");
    for (int i = 0, length = name.length(); i < length; i++) {
        char c = name.charAt(i);
        if (c <= '\u001f' || c >= '\u007f') {
            throw new IllegalArgumentException(Util.format(
              "Unexpected char %#04x at %d in header name: %s", (int) c, i, name));
        }
    }
    if (value == null) throw new NullPointerException("value == null");
    for (int i = 0, length = value.length(); i < length; i++) {
        char c = value.charAt(i);
        if (c <= '\u001f' || c >= '\u007f') {
            throw new IllegalArgumentException(Util.format(
              "Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value));
        }
    }
}

看源码,解决的方法很简单,保证所有的 head 都是符合编码要求。

但是我的情况是,其中的某一些字符不合要求,这部分服务器不关心,而本地却又不知道具体哪些是不和要求的。我的解决思路很简单,挑出不合要求的字符,把这些字符单独转码。

private static String encodeHeadInfo( String headInfo ) {
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0, length = headInfo.length(); i < length; i++) {
        char c = headInfo.charAt(i);
        if (c <= '\u001f' || c >= '\u007f') {
            stringBuffer.append( String.format ("\\u%04x", (int)c) );
        } else {
            stringBuffer.append(c);
        }
    }
    return stringBuffer.toString();
}

这样

转载于:https://blog.51cto.com/3367658/2386669

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值