okhttp特殊符号问题

okhtp3 添加header时,个别机型报错:

    java.lang.IllegalArgumentException: Unexpected char 0xc0 at 355 in authtoken value: iuf1v4z6etjbmdnB7zR8V2K6QkoVV3EIOLMRDP5jvfm1FuTjdJvl092go5fp/PF5ttKpdXpZtxvtSa1yUIRMLuIlOJAdfcpSHbJfHA/7CDpESc5tn8ur41jnbCH5MOWC4rquJD770+20jAsvx4fLTd4nZIaYvfJ5nmDd3pS8Zfp1n+7FwitYkc7aHR+OgTxVvFTXSg5abhKGhl91p9bVRVd/GRiu45ASRhlAJWkiXgFG6qF+azueqIrpmmGLXfEHa3z1mRfaPvZ/odMg7ZKdXsx8oCZdUpjZKEsS3GJM14uSCkX/JkEj8DrlhWGoYZXTZtRxN4hbpcJK2MjVIHQWDQ==|214219197|À|0|1436367305868
        at okhttp3.Headers$Builder.checkNameAndValue(Headers.java:338)
        at okhttp3.Headers$Builder.set(Headers.java:318)
        at okhttp3.Request$Builder.header(Request.java:165)
        at com.guagua.commerce.http.netService.ApiService.lambda$new$0(ApiService.java:62)
        at com.guagua.commerce.http.netService.-$$Lambda$ApiService$-u3Pv0zMuNrJyTb9k-PETiokY50.intercept(Unknown Source:0)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:929)

查找后发现是 okhttp3.Headers$Builder.checkNameAndValue 对中文进行了过滤,如果不符合条件就抛出异常IllegalArgumentException

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));
        }
      }
    }

在okhttp github 的issues中找到了解决方案:
链接地址:https://github.com/square/okhttp/issues/2802

public static String toHumanReadableAscii(String s) {
        for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
            c = s.codePointAt(i);
            if (c > '\u001f' && c < '\u007f') continue;

            Buffer buffer = new Buffer();
            buffer.writeUtf8(s, 0, i);
            for (int j = i; j < length; j += Character.charCount(c)) {
                c = s.codePointAt(j);
                buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
            }
            return buffer.readUtf8();
        }
        return s;
    }

你可能感兴趣的:(okhttp特殊符号问题)