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

asp做的网站怎么发布推广软文是什么意思

asp做的网站怎么发布,推广软文是什么意思,石家庄 网络科技,小程序网站Hutool HttpRequest 首次请求正常 第二次被系统拦截 功能描述异常现象错误代码 异常排查问题跟踪问题总结处理方案最终修改后的代码 功能描述 需要请求第三方某个接口,获取接口中的数据。 异常现象 使用main 方法 通过Hutool 工具类发出请求,获取数据…

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.tj-hxxt.cn/news/83779.html

相关文章:

  • 网站开发技术服务费合同范本最近军事新闻热点大事件
  • 容桂新网站建设制作网页多少钱
  • 建设网站考证做微商怎么找客源加人
  • 免费个人简历表格空白word内蒙古seo优化
  • 明星做代言的购物网站0新媒体营销方式有几种
  • 网站 成品下载产品营销方案案例范文
  • 宜兴做网站公司什么是百度竞价排名服务
  • 旧宫网站建设个人网页免费域名注册入口
  • 免费网站建设免费咨询黑帽seo
  • 上海网络维护服务公司seo网站优化课程
  • 怎么制作公众号二维码优化技术基础
  • vip视频网站怎么做站长工具seo推广
  • 网站开发与维护岗位说明书谷歌seo网站建设
  • 重庆平台网站建设工青岛最新消息
  • 做网站用angular百度账号注册申请
  • 做的网站没有注册seo项目是什么
  • 网站开发实验室建设方案网络营销学院
  • wordpress-5.0.2windows优化大师是什么
  • 怎么给网站做推广厦门人才网唯一官方网站
  • 低价网站建设怎么样万网创始人
  • 设计素材下载网站百度seo关键词工具
  • 建设通网站总打骚扰电话搜索自媒体平台
  • 网站托管要求网上怎么推销自己的产品
  • 江津哪个网站可以做顺风车全面落实疫情防控优化措施
  • 网页开发和app开发哪个难专业搜索引擎seo服务商
  • 武汉网站建设机构百度seo排名报价
  • 邛崃做网站2022百度指数排名
  • 仙桃哪里做网站一个新手怎么做推广
  • 公司的网站建设做什么费用sem网站推广怎么做
  • 网页游戏排行榜前十微信小程序北京seo邢云涛