NoHttpResponseException异常解决优化HttpClient配置以避免连接问题

 更新时间:2023年10月12日 09:23:33   作者:codecraft  
这篇文章主要为大家介绍了NoHttpResponseException异常解决,优化HttpClient配置以避免连接问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下HttpClient的NoHttpResponseException

NoHttpResponseException

org/apache/http/NoHttpResponseException.java

/**
 * Signals that the target server failed to respond with a valid HTTP response.
 *
 * @since 4.0
 */
public class NoHttpResponseException extends IOException {
    private static final long serialVersionUID = -7658940387386078766L;
    /**
     * Creates a new NoHttpResponseException with the specified detail message.
     *
     * @param message exception message
     */
    public NoHttpResponseException(final String message) {
        super(HttpException.clean(message));
    }
}
NoHttpResponseException继承了IOException,用于表示目标服务器没有返回一个正常的http response

DefaultHttpResponseParser

org/apache/http/impl/conn/DefaultHttpResponseParser.java

public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
    private final Log log = LogFactory.getLog(getClass());
    private final HttpResponseFactory responseFactory;
    private final CharArrayBuffer lineBuf;
    //......
    @Override
    protected HttpResponse parseHead(
            final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
        //read out the HTTP status string
        int count = 0;
        ParserCursor cursor = null;
        do {
            // clear the buffer
            this.lineBuf.clear();
            final int i = sessionBuffer.readLine(this.lineBuf);
            if (i == -1 && count == 0) {
                // The server just dropped connection on us
                throw new NoHttpResponseException("The target server failed to respond");
            }
            cursor = new ParserCursor(0, this.lineBuf.length());
            if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
                // Got one
                break;
            } else if (i == -1 || reject(this.lineBuf, count)) {
                // Giving up
                throw new ProtocolException("The server failed to respond with a " +
                        "valid HTTP response");
            }
            if (this.log.isDebugEnabled()) {
                this.log.debug("Garbage in response: " + this.lineBuf.toString());
            }
            count++;
        } while(true);
        //create the status line from the status string
        final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
        return this.responseFactory.newHttpResponse(statusline, null);
    }
    protected boolean reject(final CharArrayBuffer line, final int count) {
        return false;
    }
}
DefaultHttpResponseParser继承了AbstractMessageParser,其parseHead方法读取sessionBuffer,若该数据为空则抛出NoHttpResponseException("The target server failed to respond")

小结

NoHttpResponseException继承了IOException,用于表示目标服务器没有返回一个正常的http response,一般是目标服务器负载太高处理不过来因而断开了连接,也有可能是目标服务器把这个空闲连接关闭了,而HttpClient则继续用这个连接发送请求则会读取不到正常的reponse,因而抛出NoHttpResponseException。大多数情况下,可以通过重试解决。另外针对因为keep-alive超时断开的,可以配置HttpClient的connTimeToLive值小于服务端的keepAlive值(通常是60s)。

doc

以上就是NoHttpResponseException异常解决优化HttpClient配置以避免连接问题的详细内容,更多关于HttpClient NoHttpResponseException的资料请关注脚本之家其它相关文章!

相关文章

  • springboot实现调用百度ocr实现身份识别+二要素校验功能

    springboot实现调用百度ocr实现身份识别+二要素校验功能

    本文介绍了如何使用Spring Boot调用百度OCR服务进行身份识别,并通过二要素校验确保信息准确性,感兴趣的朋友一起看看吧
    2025-03-03
  • Dubbo扩展点SPI实践示例解析

    Dubbo扩展点SPI实践示例解析

    这篇文章主要为大家介绍了Dubbo扩展点SPI实践示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Java实战员工绩效管理系统的实现流程

    Java实战员工绩效管理系统的实现流程

    只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+SSM+Mysql+Maven+HTML实现一个员工绩效管理系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • Java基础之this关键字的使用

    Java基础之this关键字的使用

    今天给大家带来的是关于Java基础的相关知识,文章围绕着this关键字的使用展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Java后端配置允许跨域方式

    Java后端配置允许跨域方式

    本文介绍了在不同技术和框架中配置跨域资源共享(CORS)的方法,包括使用SpringMVC的@CrossOrigin注解、SpringBoot的全局CORS配置、SpringSecurity中的CORS集成以及手动设置响应头,根据具体需求和技术栈,选择合适的方法来确保跨域请求的安全性和有效性
    2025-02-02
  • Java比较两个对象大小的三种方法详解

    Java比较两个对象大小的三种方法详解

    在优先级队列中插入的元素必须能比较大小,如果不能比较大小,如插入两个学生类型的元素,会报ClassCastException异常。本文就为大家总结了Java比较两个对象大小的三种方法,需要的可以参考一下
    2022-07-07
  • mybatis中的异常BindingException详解

    mybatis中的异常BindingException详解

    这篇文章主要介绍了mybatis中的异常BindingException详解,此异常是mybatis中抛出的,意思是使用的这个方法找到,但是因为mapperScan()已经扫描到了Mapper类了,在绑定Mapper.xml时没有绑定到导致的,需要的朋友可以参考下
    2024-01-01
  • Spring中的@CrossOrigin注解的使用详细解读

    Spring中的@CrossOrigin注解的使用详细解读

    这篇文章主要介绍了Spring中的@CrossOrigin注解的使用详细解读,跨源资源共享(CORS),是由大多数浏览器实现的W3C规范,允许对跨域请求进行灵活授权,用来代替IFRAME或JSONP等非正规实现方式,需要的朋友可以参考下
    2023-11-11
  • 详解Java中的hashcode

    详解Java中的hashcode

    这篇文章主要介绍了详解Java中的hashcode,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-05-05
  • Struts2 OGNL表达式实例详解

    Struts2 OGNL表达式实例详解

    这篇文章主要介绍了Struts2 OGNL表达式实例详解,相关实例代码,需要的朋友可以参考。
    2017-09-09

最新评论