最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Okhttp去除請求頭user-agent

 更新時間:2017年12月11日 13:42:52   作者:Arison  
本篇文章主要介紹了淺談Okhttp去除請求頭user-agent,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Http Header之User-Agent 

User-Agent中文名為用戶代理,是Http協(xié)議中的一部分,屬于頭域的組成部分,User Agent頁簡稱UA。她是一個特殊字符串頭,是一種想訪問網(wǎng)站提供你說使用的瀏覽器類型和版本,操作系統(tǒng)和版本,瀏覽器內(nèi)核等信息的標(biāo)識,用戶所訪問的網(wǎng)站可以顯示不同的排版,而為用戶提供更好的體驗(yàn)或者進(jìn)行信息統(tǒng)計(jì)

現(xiàn)象說明

用OKhttp框架請求http請求的時候會把user-agent帶上;然而有些時候我們需要把請求頭里面的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"
}

你會發(fā)現(xiàn): "user-agent":"okhttp/3.4.1" 這是由于okhttp內(nèi)置攔截器BridgeInterceptor默認(rèn)添加的。我們需要借助網(wǎng)絡(luò)攔截器來重新攔截請求頭;

解決方法:

利用攔截器來實(shí)現(xiàn)對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ò)請求:

{
"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是返回請求所有參數(shù)和請求頭信息。代碼就不展示出來了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評論

左贡县| 姚安县| 登封市| 定安县| 台安县| 富平县| 浦东新区| 定西市| 清水河县| 乌拉特后旗| 乐清市| 枣强县| 北川| 突泉县| 革吉县| 周口市| 闽侯县| 兴隆县| 准格尔旗| 宜宾县| 湘潭县| 海林市| 衡阳市| 兴安县| 静乐县| 温宿县| 黄浦区| 安图县| 博白县| 纳雍县| 德保县| 涪陵区| 大厂| 宁化县| 芜湖县| 曲靖市| 日土县| 永平县| 本溪| 扎鲁特旗| 大名县|