咱們知道OkHttp走的並非原生的http請求,所以在header裏面並無真正的User-Agent,而是「okhttp/版本號」這樣的字符串,由於後臺須要統計信息,要求傳入正確的User-Agent,那麼咱們如何獲取User-Agent並設置給Okhttp呢?java
private static String getUserAgent() { String userAgent = ""; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { try { userAgent = WebSettings.getDefaultUserAgent(context); } 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. 在Android 17以後能夠經過WebSettings.getDefaultUserAgent(context)
獲取,可是通過測試極個別手機會出現找不到類的狀況,所以try-catch一下,那麼第二種方式是System.getProperty("http.agent")
,這兩種方式有什麼不一樣呢?從結果上來看是第一種獲得的信息更全一點;
第一種:android
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
第二種:app
Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 4LTE MIUI/6.10.13)
二、在一些國產手機上面這個User-Agent裏面會包含中文,就會報錯oop
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)); } } }
所以上段代碼纔會對返回結果進行過濾,若是不符合條件,會進行轉碼。ui
Request request = new Request.Builder().url(url).removeHeader("User-Agent").addHeader("User-Agent", getUserAgent()).build(); httpClient.newCall(request).enqueue(handler);