新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解okhttp3請求頭不能為中文的坑-創(chuàng)新互聯(lián)
對源碼有一定的了解,以為基本可以駕馭了,誰知道,坑來了。
問題描述:
上線接口的某一天,有用戶報障,說登錄不上,而且陸續(xù)有報障。
通過log 發(fā)現(xiàn)登錄不上,都有一個類似的報錯:
“java.lang.IllegalArgumentException: Unexpected char 0x514d at 4 in wifiSsid value: "360免費WiFi-DP" at ”
什么?怎么和wifiSsid有關(guān)系了?后來查了一下代碼,發(fā)現(xiàn)確實在登錄接口上通過請求頭的形式上傳了 wifissid, 也就是連接的wifi的名字。Exception描述是第四個 char ,也就是“免”字?難道是因為中文的原因?當(dāng)時是這么猜測的。
源碼查詢
猜測并不能作為判斷的標(biāo)準(zhǔn),然后做了一下測試,果然連接含義中文名字的wifi就有問題,也就是不能添加中文到請求頭里面。這是為什么呢?最后還是通過源碼進(jìn)行的求證:
在okhttp的源碼Header.java,發(fā)現(xiàn)set 和add header, 都會有這個判斷:
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 <= '\u0020' || 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 != '\t') || c >= '\u007f') { throw new IllegalArgumentException(Util.format( "Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value)); } } }
文章標(biāo)題:詳解okhttp3請求頭不能為中文的坑-創(chuàng)新互聯(lián)
鏈接地址:http://fisionsoft.com.cn/article/hhioi.html