当前位置: 首页 > news >正文

Hutool HttpRequest 首次请求正常 第二次被系统拦截

Hutool HttpRequest 首次请求正常 第二次被系统拦截

  • 功能描述
    • 异常现象
      • 错误代码
    • 异常排查
    • 问题跟踪
    • 问题总结
    • 处理方案
    • 最终修改后的代码

功能描述

需要请求第三方某个接口,获取接口中的数据。

异常现象

使用main 方法 通过Hutool 工具类发出请求,获取数据信息时,发现第一次请求接口可以正常获取数据项,但是循环遍历请求接口时,除首次请求外,其他请求都被第三方接口拦截,提示需要登录。

错误代码

String url = "http://xxx/kk/hh/f?page=1&limit=15";for(int i = 0;i<10;i++){HttpRequest http = HttpRequest.get(url);http.header("Accept", "application/json, text/javascript, */*; q=0.01").header("Accept-Encoding", "gzip, deflate, br").header("Connection", "keep-alive").header("Cookie", "kkid=sdf456sadf45dsf6ds4f; Token=15sd4f5ds6ads54f5sdf45dsf")HttpResponse tt = http.execute();String body = tt.body();}

异常排查

1.由于每次的首次请求都可以成功,排除接口无法请求或做了防重复请求之类的限制。
2.使用 java.net.HttpURLConnection 请求可以正常使用,再次排除接口问题,同时锁定可能是Hutool的问题

问题跟踪

1.跟踪Hutool HttpRequest 的 execute() -> doExecute();

	// 最终执行到这个方法private HttpResponse doExecute(boolean isAsync, Chain<HttpRequest> requestInterceptors, Chain<HttpResponse> responseInterceptors) {// 请求前的拦截方法,可以实现该方法,对请求拦截后处理if (null != requestInterceptors) {Iterator var4 = requestInterceptors.iterator();while(var4.hasNext()) {HttpInterceptor<HttpRequest> interceptor = (HttpInterceptor)var4.next();interceptor.process(this);}}// 获取请求参数this.urlWithParamIfGet();// 初始化连接,此次问题出现在改方法中this.initConnection();// 发送请求this.send();//接受响应信息HttpResponse httpResponse = this.sendRedirectIfPossible(isAsync);if (null == httpResponse) {httpResponse = new HttpResponse(this.httpConnection, this.config, this.charset, isAsync, this.isIgnoreResponseBody());}// 请求响应体拦截,如果项对响应信息做处理,可以实现该方法if (null != responseInterceptors) {Iterator var7 = responseInterceptors.iterator();while(var7.hasNext()) {HttpInterceptor<HttpResponse> interceptor = (HttpInterceptor)var7.next();interceptor.process(httpResponse);}}return httpResponse;}

2.根据首次和其他次的请求,锁定了原因是请求头中的cookie不一致导致,上述方法中的this.initConnection()方法。

	// 初始化httpConnectionprivate void initConnection() {// 判断当前hutool的httpConnection 是否不为空,不为空则关闭连接if (null != this.httpConnection) {this.httpConnection.disconnectQuietly();}// 创建一个新的httpConnection this.httpConnection = HttpConnection.create(this.url.setCharset(this.charset).toURL(this.urlHandler), this.config.proxy).setConnectTimeout(this.config.connectionTimeout).setReadTimeout(this.config.readTimeout).setMethod(this.method).setHttpsInfo(this.config.hostnameVerifier, this.config.ssf).setInstanceFollowRedirects(false).setChunkedStreamingMode(this.config.blockSize).header(this.headers, true);// 判断cookie 是否为空,不为空就设置cookieif (null != this.cookie) {this.httpConnection.setCookie(this.cookie);} else { // 否则就从已经建立的连接中获取cookie(响应体的coolie),添加到cookie中// 由于此处我的cookie 为空,所以直接执行此处,此方法将上一次请求中响应回来的cookie,自动设置到我下一次的请求的请求头中,导致请求头中 key 为Cookie中,导致请求头中有三个key为Cookie的参数,从而导致第三方接口取cookie 时异常,判断非法。GlobalCookieManager.add(this.httpConnection);}if (this.config.isDisableCache) {this.httpConnection.disableCache();}}

3.由于上述方法的cookie 为空,所以直接执行GlobalCookieManager.add(this.httpConnection),此方法将上一次请求中响应回来的cookie,自动设置到我下一次的请求的请求头中,导致请求头中 key 为Cookie中,导致请求头中有三个key为Cookie的参数,从而导致第三方接口取cookie 时异常,判断非法。

	// 添加全局Cookiepublic static void add(HttpConnection conn) {// 判断cookie 管理器是否为空,为空则不对cookie进行操作,此处便是解决问题的关键。if (null != cookieManager) {Map cookieHeader;try {// 从连接中获取上次响应体返回的cookiecookieHeader = cookieManager.get(getURI(conn), new HashMap(0));} catch (IOException var3) {throw new IORuntimeException(var3);}// 将其添加至请求头中,使用的是addRequestProperty,不是setRequestPropertyconn.header(cookieHeader, false);}}

问题总结

Hutool HttpRequest 将上一次请求中响应回来的cookie 添加到下一次的请求头中(key为cookie),
导致第三方使用cookie 验证登录信息的地方失效,从而请求被拦截

处理方案

将 问题跟踪-> 步骤3中 cookieManager 对象设置为空,使其跳过 cookie 设置
由于cookieManager 是个静态对象,其类GlobalCookieManager 中提供setCookieManager 方法
HttpRequest 中也提供 了关闭cookie 的方法(此关闭cookie,就是让cookieManager 对象为空的动作),所以处理方案有两个,经测试,两者都可以实现关闭操作

  1. GlobalCookieManager.setCookieManager(null);
  2. HttpRequest .closeCookie();

最终修改后的代码

String url = "http://xxx/kk/hh/f?page=1&limit=15";
// 此方法为全局方法,如果确实需要此操作,在需要的地方还需再次设置cookieManager  对象HttpRequest .closeCookie()for(int i = 0;i<10;i++){HttpRequest http = HttpRequest.get(url);http.header("Accept", "application/json, text/javascript, */*; q=0.01").header("Accept-Encoding", "gzip, deflate, br").header("Connection", "keep-alive").header("Cookie", "kkid=sdf456sadf45dsf6ds4f; Token=15sd4f5ds6ads54f5sdf45dsf")HttpResponse tt = http.execute();String body = tt.body();}
http://www.lryc.cn/news/241731.html

相关文章:

  • github国内访问小解(windows)
  • NX二次开发UF_CSYS_set_wcs_display 函数介绍
  • DNS 区域传输 (AXFR)
  • Ubuntu 安装 JMeter:轻松上手
  • 在工业生产环境下,服务器没有互联网,如何通过代理自己的电脑上互联网?
  • 【brpc学习实践六】backup request场景案例
  • el-table导出为excel表格
  • CVE-2022-0543(Redis 沙盒逃逸漏洞)
  • 查swap内存使用
  • Element UI的Tabs 标签页位置导航栏去除线条
  • 【Python 训练营】N_1 验证密码
  • Pinia 和 Vuex 的对比,storeToRefs 的原理
  • Mycat分库分表的操作(配置)
  • android的canvas的clipRegion废弃替代代码
  • KubeSphere 社区双周报 | Fluent Operator 2.6.0 发布 | 2023.11.10-11.23
  • 【通俗易懂】git原理、安装及连接gitlab,github
  • TCP /UDP协议的 socket 调用的过程
  • 外贸独立站外部优化:提升网站可见度与吸引力的策略
  • buildAdmin 后端控制器的代码分析
  • Python丨让简历脱颖而出的关键,居然是“它”!
  • CMake中常见的预定义变量
  • .netcore 获取appsettings
  • 额温枪方案,MS8551,MS8601;MS1112,MS1100
  • 数字图像处理基础-用通俗语言进行超详细的总结
  • 3.3.1详解linux内核链表list_head及其接口应用
  • 发挥云计算潜力:Amazon Lightsail 与 Amazon EC2 的综述
  • 【深度学习】卷积神经网络(CNN)
  • 科普:多领域分布式协同仿真
  • openstack(2)
  • Jmeter 压测保姆级入门教程