异常信息

org.apache.http.NoHttpResponseException xxx failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)

其实这个异常是偶发的,就是因为偶发所以不太好查,主要是本地不好复现,只在服务器压测时有问题

上网找了些解答,但说的其实不太清楚,最后再stackoverflow找到答案了

https://stackoverflow.com/questions/10558791/apache-httpclient-interim-error-nohttpresponseexception#

解决方案其实在官方文档里也有解答 http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

主要是这段

2.5. Connection eviction policy

One of the major shortcomings of the classic blocking I/O model is that the network socket can react to I/O events only when blocked in an I/O operation. When a connection is released back to the manager, it can be kept alive however it is unable to monitor the status of the socket and react to any I/O events. If the connection gets closed on the server side, the client side connection is unable to detect the change in the connection state (and react appropriately by closing the socket on its end).

HttpClient tries to mitigate the problem by testing whether the connection is 'stale', that is no longer valid because it was closed on the server side, prior to using the connection for executing an HTTP request. The stale connection check is not 100% reliable. The only feasible solution that does not involve a one thread per socket model for idle connections is a dedicated monitor thread used to evict connections that are considered expired due to a long period of inactivity. The monitor thread can periodically call ClientConnectionManager#closeExpiredConnections() method to close all expired connections and evict closed connections from the pool. It can also optionally call ClientConnectionManager#closeIdleConnections() method to close all connections that have been idle over a given period of time.

这个异常的原因就是客户端创建了到服务端的连接,但是服务端会在连接空闲时关闭这个连接,而HttpClient无法感知这个事件,所以继续使用这个链接就报错了,毕竟那边都断了

Most likely persistent connections that are kept alive by the connection manager become stale. That is, the target server shuts down the connection on its end without HttpClient being able to react to that event, while the connection is being idle, thus rendering the connection half-closed or 'stale'.

解决方案其实就是配置两个参数,使用httpclient的话基本都会用到线程池

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.closeExpiredConnections();
//这里的空闲时间要和keepalive配合使用,比如keepalive配置20s,那这里的关闭空闲时间最好配置15sconnectionManager.closeIdleConnections(closeIdleTime, TimeUnit.SECONDS);
connectionManager.closeExpiredConnections();  关闭过期链接
connectionManager.closeIdleConnections(closeIdleTime, TimeUnit.SECONDS);  隔多久关闭空闲状态的链接

使用httpclient时报错NoHttpResponseException failed to respond相关推荐

  1. 【Ubuntu】制作usb启动盘安装ubuntu18.04时报错:“failed to load ldlinux.c32”

    1.问题描述 使用UltraISO制作ubuntu18.04操作系统的usb启动盘后,按照时报错: failed to load ldlinux.c32 ubuntu16.04.ubuntu14.04 ...

  2. spring集成kafka运行时报错:Failed to construct kafka producer] with root cause

    spring集成kafka运行时报错:Failed to construct kafka producer] with root cause org.apache.kafka.common.Kafka ...

  3. 解决安装python3.7.2时报错Setup failed,0x80072f7d-未指定的错误

    解决安装python3.7.2时报错Setup failed,0x80072f7d-未指定的错误 之前win7安装过python,很顺利的安装成功.但是系统升级后再次安装就安装不上了,总是报错,百度了 ...

  4. 执行wsl 命令时报错 nsenter: failed to execute 564: No such file or directory

    问题 在cmd命令框中执行wsl命令时报错 nsenter: failed to execute 564: No such file or directory无法进入Ubuntu子系统中 C:\Use ...

  5. docker端口映射或启动容器时报错 driver failed programming external connectivity on endpoint quirky_allen

    docker端口映射或启动容器时报错  Error response from daemon: driver failed programming external connectivity on e ...

  6. docker端口映射或启动容器时报错 driver failed programming external connectivity on endpoint

    docker端口映射或启动容器时报错 Error response from daemon: driver failed programming external connectivity on en ...

  7. 前端项目cnpm install时报错,Failed to connect to github.com port 443: Connection refused

    前端项目,在cnpm install时报错,提示:Failed to connect to github.com port 443: Connection refused 解决方法: 1.查找gith ...

  8. git将本地文件推送到远程仓库时报错: error: failed to push some refs to ‘https://gitee.com/no-talent-all-by-hand/dome

    今天使用Git推送本地文件到远程库的时候一直报错 error: failed to push some refs to 'https://gitee.com/no-talent-all-by-hand ...

  9. springboot项目进行 mvn install 时报错 repackage failed: Unable to find main class 处理记录

    异常描述:springboot项目进行 mvn install时 报错如下 Failed to execute goal org.springframework.boot:spring-boot-ma ...

最新文章

  1. nginx 只写了listen80 没有 listen443 用https访问
  2. 企业微信推送消息延迟_iPhone手机微信推送消息总是延迟怎么办?
  3. mysql count 排序_SQL进阶排序和窗口函数
  4. finally块的问题(finally block does not complete normally) (转)
  5. Ajax-基础篇(02)
  6. C++STL特殊容器priority_queue
  7. Lottie-iOS的应用及部分源码分析
  8. SPLUNK 安装配置及常用语法
  9. scipy中稀疏矩阵coo_matrix, csr_matrix 的使用
  10. 制造业供应链平台解决方案,实现数字化、可视化、信息化
  11. 20200209毕设日记-开题报告
  12. 软件工程--构建之法--功能测试 设计10个或者更多的测试案例完成对钉书钉的功能测试...
  13. VMware虚拟机安装win10系统教程(巨细)
  14. 内存的永久保存区内存溢出解决
  15. python 按照四个点的坐标裁剪不规则四边形
  16. 祝所有高考学子高考加油!
  17. linux 基础常用命令集
  18. 本草纲目pdf彩图版下载_本草纲目彩色插图版 完整版(PDF格式)
  19. java 枚举 color_java 枚举实现方法-java 枚举个性化-嗨客网
  20. 【安卓】[Android]添加google账户 - 您的用户名和密码不匹配,请重试

热门文章

  1. 金蝶首席用户体验官对“用户体验”的思考
  2. matplotlib——坐标负数显示问题Unicode minus
  3. Tencent SSL 数字证书安装部署
  4. 山东科技大学计算机篮球,球队资料:山东科技大学男子篮球队
  5. 智力风暴(经典智力题)
  6. 【SIM】MCC(移动国家码)和 MNC(移动网络码)
  7. Java创建学生喂养动物类
  8. 从零开始学习Java设计模式 | 设计模式入门篇:设计模式概述
  9. springmvc接收用户提交的数据
  10. 计算机无法连接iphone,iphone6连接电脑没反应怎么回事?iphone6连接电脑没反应的解决方法...