亲宝软件园·资讯

展开

Okhttp去除 user-agent 浅谈Okhttp去除请求头user-agent

Arison 人气:4
想了解浅谈Okhttp去除请求头user-agent的相关内容吗,Arison在本文为您仔细讲解Okhttp去除 user-agent的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Okhttp去除,user-agent,Okhttp,user-agent,下面大家一起来学习吧。

Http Header之User-Agent 

User-Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent页简称UA。她是一个特殊字符串头,是一种想访问网站提供你说使用的浏览器类型和版本,操作系统和版本,浏览器内核等信息的标识,用户所访问的网站可以显示不同的排版,而为用户提供更好的体验或者进行信息统计

现象说明

用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);

        }
      }));
}

返回结果:

{
"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"
}

你会发现: "user-agent":"okhttp/3.4.1" 这是由于okhttp内置拦截器BridgeInterceptor默认添加的。我们需要借助网络拦截器来重新拦截请求头;

解决方法:

利用拦截器来实现对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();

然后利用网络拦截器来设置自定义的拦截器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);//应用拦截器
  okBuilder.addNetworkInterceptor(logInterceptor);//网络拦截器

最后重新发送网络请求:

{
"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是返回请求所有参数和请求头信息。代码就不展示出来了。

加载全部内容

相关教程
猜你喜欢
用户评论