正文  软件开发 > 编程综合 >

Okhttp设置User-Agent遇到的坑

我们知道Okhttp走的并不是原生的http请求,因此他在header里面并没有真正的User-Agent,而是“okhttp/版本号”这样的字符串,因为后台需要统计信息,要求传入正确的...

我们知道Okhttp走的并不是原生的http请求,因此他在header里面并没有真正的User-Agent,而是“okhttp/版本号”这样的字符串,因为后台需要统计信息,要求传入正确的User-Agent,那么我们如何获取User-Agent并设置给Okhttp呢?

0x01-正确获取useragent

private static String getUserAgent() {
        String userAgent = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            try {
                userAgent = WebSettings.getDefaultUserAgent(AllyApp.getInstance());
            } catch (Exception e) {
                userAgent = System.getProperty("http.agent");
            }
        } else {
            userAgent = System.getProperty("http.agent");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0, length = userAgent.length(); i < length; i++) {
            char c = userAgent.charAt(i);
            if (c <= '\u001f' || c >= '\u007f') {
                sb.append(String.format("\\u%04x", (int) c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

代码说明:

1、在Api17之后可以通过 WebSettings.getDefaultUserAgent(AllyApp.getInstance()) 获取,但是经过测试极个别手机会出现找不到类的情况,因此try-catch一下,那么第二种方式是 System.getProperty("http.agent") ,这两种方式有什么不同呢?从结果上来看是第一种得到的信息更全一点;

第一种:

Mozilla/5.0 (Linux; Android 6.0.1; MI 4LTE Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) 
Version/4.0 Chrome/51.0.2704.81 Mobile Safari/537.36

第二种:

Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 4LTE MIUI/6.10.13)

2、在一些国产手机上面这个User-Agent里面会包含中文,就会报错

java.lang.IllegalArgumentException: Unexpected char 0x7231 at 33 in User-Agent value: Mozilla/5.0 (Linux; Android 5.1; 爱国者-X86 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Unexpected char 0x7231 at 33 in User-Agent value: Mozilla/5.0 
(Linux; Android 5.1; 爱国者-X86 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36
at okhttp3.Headers$Builder.checkNameAndValue(Headers.java:320)
at okhttp3.Headers$Builder.add(Headers.java:270)
at okhttp3.Request$Builder.addHeader(Request.java:175)

什么原因引起的呢? okhttp3.Headers$Builder.checkNameAndValue 进到这个方法里面可以看到okhttp对中文进行了过滤,如果不是中文就抛出异常 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));
        }
      }
    }

因此上段代码才会对返回结果进行过滤,如果不符合条件,会进行转码。

0x02-给Okhttp设置User-Agent

Request request = new Request.Builder().url(url).removeHeader("User-Agent").addHeader("User-Agent",
                getUserAgent()).build();
httpClient.newCall(request).enqueue(handler);

来自:http://www.jianshu.com/p/ddbe8c637fc5