• 聊聊HttpClient的NoHttpResponseException


    本文主要研究一下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));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    NoHttpResponseException继承了IOException,用于表示目标服务器没有返回一个正常的http response

    DefaultHttpResponseParser

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

    public class DefaultHttpResponseParser extends AbstractMessageParser {
    
        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;
        }
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    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

  • 相关阅读:
    子网的划分
    1-STM32之GPIO点亮LED
    Node.js 调用 fluent-ffmpeg
    Ubuntu安装NVIDIA显卡驱动
    我和EarthSDK Cesium那点事(零)
    网工内推 | 上市公司,云平台运维,IP认证优先,13薪
    华为云应用中间件DCS系列—Redis实现(电商网站)秒杀抢购示例
    Golang 中的字符串:常见错误和最佳实践
    Go 语言之 Json 解析,正则表达式,从数据库导出 CSV 文件代码示例
    ROS 话题通信(C++)
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/133779426