Okhttp去除请求头user-agent

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/78741663

现象说明

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

参考文献:

联系方式:

相关文章
|
2月前
|
安全 数据库
16、HTTP头注入(User-Agent、Referer)
16、HTTP头注入(User-Agent、Referer)
21 0
|
4月前
|
数据采集 监控 机器人
User-Agent 即用户代理
User-Agent 即用户代理
121 2
|
安全 Java 应用服务中间件
使用OkHttp工具时Authorization请求头丢失问题
记一次联调三方接口时&quot;Authorization&quot;请求头丢失问题, 使用工具OkHttp
使用OkHttp工具时Authorization请求头丢失问题
|
2月前
|
安全 数据库
小课堂 -- HTTP头注入(User-Agent、Refer)
小课堂 -- HTTP头注入(User-Agent、Refer)
14 0
|
7月前
|
前端开发 Java
java在过滤器中为http请求加请求头header
现在有一个需求场景是,每一个请求我都需要在请求头里面加上token这个请求头,作为一种校验机制,传统的接口可以通过设置一个全局的变量,然后通过页面携带过来(大概就是先将我们的token放在session中,写一个服务用来获取session中的token,然后主页面用ajax调用接口,将token放在隐藏域中,然后将请求头放进来,用ajax方法,这里不想洗说了),但是有一种情况是通过页面传递的并不一定都会适用所有接口,比如上传和下载的接口有时候头里面就没有token参数,可能是上传和下载是用表单提交的 这个时候如何将请求头通过后台的方法加进来? 想到用过滤器,用后台方法强制加入请求头。
91 0
|
6月前
|
Web App开发 JSON 数据格式
使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
101 0
|
8月前
|
测试技术 数据格式 Python
requests--请求头设置
requests--请求头设置
|
8月前
|
JSON 数据格式
requests--重定向,序列化
requests--重定向,序列化
|
8月前
|
Web App开发 数据采集 安全
网络爬虫请求头中的Referer和User-Agent与代理IP的配合使用
网络爬虫请求头中的Referer和User-Agent与代理IP的配合使用
Golang:user_agent解析HTTP用户代理User Agents
Golang:user_agent解析HTTP用户代理User Agents
162 0