您好,登錄后才能下訂單哦!
小編給大家分享一下Okhttp如何去除請(qǐng)求頭user-agent,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Http Header之User-Agent
User-Agent中文名為用戶代理,是Http協(xié)議中的一部分,屬于頭域的組成部分,User Agent頁(yè)簡(jiǎn)稱UA。她是一個(gè)特殊字符串頭,是一種想訪問網(wǎng)站提供你說使用的瀏覽器類型和版本,操作系統(tǒng)和版本,瀏覽器內(nèi)核等信息的標(biāo)識(shí),用戶所訪問的網(wǎng)站可以顯示不同的排版,而為用戶提供更好的體驗(yàn)或者進(jìn)行信息統(tǒng)計(jì)
現(xiàn)象說明
用OKhttp框架請(qǐng)求http請(qǐng)求的時(shí)候會(huì)把user-agent帶上;然而有些時(shí)候我們需要把請(qǐng)求頭里面的user-agent去掉;
客戶端程序:
private static final String BASE_URL = "http://192.168.253.200:8080/Chapter/"; public static void testHeaders() { HttpClient httpClient = new HttpClient.Builder(BASE_URL).isDebug(false) .add("header", "12345") .header("master", "china") .connectTimeout(5000) .readTimeout(5000) .build(); httpClient.Api().send(new HttpClient.Builder().url("postParam") .add("header", "123459") .add("master", "usa") .add("token", "388298a0c89f4a38b2fed4cd4123d441") .method(Method.POST) .build(), new ResultSubscriber<>(new ResultListener<Object>() { @Override public void onResponse(Object t) { System.out.println(t); } })); }
返回結(jié)果:
{ "headers":{ "content-length":"76", "host":"192.168.253.200:8080", "client":"Android Client", "content-type":"application/x-www-form-urlencoded", "connection":"Keep-Alive", "accept-encoding":"gzip", "master":"china", "user-agent":"okhttp/3.4.1" }, "header":"123459", "master":"usa", "token":"388298a0c89f4a38b2fed4cd4123d441" }
你會(huì)發(fā)現(xiàn): "user-agent":"okhttp/3.4.1" 這是由于okhttp內(nèi)置攔截器BridgeInterceptor默認(rèn)添加的。我們需要借助網(wǎng)絡(luò)攔截器來重新攔截請(qǐng)求頭;
解決方法:
利用攔截器來實(shí)現(xiàn)對(duì)user-agent刪除
public class LogInterceptor implements Interceptor { private HttpClient builder; @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request =request.newBuilder().removeHeader("User-Agent").build(); Response response = chain.proceed(request); okhttp3.MediaType mediaType = response.body().contentType(); String content = response.body().string(); return response.newBuilder() .body(okhttp3.ResponseBody.create(mediaType, content)) .build();
然后利用網(wǎng)絡(luò)攔截器來設(shè)置自定義的攔截器LogInterceptor :
Builder okBuilder = new OkHttpClient.Builder() .connectTimeout(mbuilder.getConnectTimeout(), TimeUnit.SECONDS) .readTimeout(mbuilder.getReadTimeout(), TimeUnit.SECONDS) .writeTimeout(mbuilder.getWriteTimeout(), TimeUnit.SECONDS) .sslSocketFactory(OkhttpUtils.createSSLSocketFactory(), new OkhttpUtils. TrustAllCerts())// 信任所有證書 .hostnameVerifier(new OkhttpUtils.TrustAllHostnameVerifier()); LogInterceptor logInterceptor = new LogInterceptor(); logInterceptor.setBuilder(mbuilder); //okBuilder.addInterceptor(logInterceptor);//應(yīng)用攔截器 okBuilder.addNetworkInterceptor(logInterceptor);//網(wǎng)絡(luò)攔截器
最后重新發(fā)送網(wǎng)絡(luò)請(qǐng)求:
{ "headers":{ "content-length":"63", "host":"192.168.253.200:8080", "client":"Android Client", "content-type":"application/x-www-form-urlencoded", "connection":"Keep-Alive", "accept-encoding":"gzip", "master":"china" }, "header":"123459", "master":"usa", "token":"388298a0c89f4a38b2fed4cd4123d441" }
說明:接口postParam是返回請(qǐng)求所有參數(shù)和請(qǐng)求頭信息。代碼就不展示出來了。
以上是“Okhttp如何去除請(qǐng)求頭user-agent”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。