前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊HttpClient的NoHttpResponseException

聊聊HttpClient的NoHttpResponseException

原创
作者头像
code4it
发布于 2023-10-11 13:34:40
发布于 2023-10-11 13:34:40
1.3K00
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下HttpClient的NoHttpResponseException

NoHttpResponseException

org/apache/http/NoHttpResponseException.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
再聊HttpClient的NoHttpResponseException
org/apache/http/protocol/HttpRequestExecutor.java
code4it
2024/04/28
3440
聊聊HttpClient的重试机制
org/apache/http/client/HttpRequestRetryHandler.java
code4it
2023/10/12
8860
聊聊HttpClient的KeepAlive
org/apache/http/conn/ConnectionKeepAliveStrategy.java
code4it
2023/10/10
9590
聊聊HttpClient的ResponseHandler
org/apache/http/client/ResponseHandler.java
code4it
2023/10/09
4410
Apache HttpClient 5 使用详细教程
超文本传输协议(HTTP)可能是当今互联网上最重要的协议之一,Web 服务、微服务以及支持网络的各种设备上的服务几乎都是 HTTP 协议,HTTP 协议已经从 Web 浏览器走向了更广泛的使用场景。
未读代码
2022/11/21
6.9K0
Apache HttpClient 5 使用详细教程
聊聊HttpClient的close
org/apache/http/impl/client/CloseableHttpClient.java
code4it
2023/10/26
5190
聊聊httpclient的connect
org/apache/http/conn/HttpClientConnectionOperator.java
code4it
2023/11/27
1480
聊聊httpclient的connect
HttpComponents HttpClient连接池(5)-可用性检查
在上一篇文章里我们介绍了 httpclient 连接池中连接的重用,以及连接的 keep alive ,在这里我们主要介绍连接的可用性检查。
TA码字
2020/04/01
1.7K0
聊聊HttpClient的DnsResolver
org/apache/http/impl/conn/InMemoryDnsResolver.java
code4it
2023/10/17
4130
聊聊HttpClient的RedirectStrategy
org/apache/http/client/RedirectStrategy.java
code4it
2023/10/18
3310
HttpComponents HttpClient连接池(7)-重试
在上一篇文章里我们介绍了 httpclient 连接池中空闲连接的清理,在这里我们主要介绍 http 连接的重试机制。
TA码字
2020/04/07
1.9K0
聊聊httpclient的ConnectionHolder
org/apache/http/conn/ConnectionReleaseTrigger.java
code4it
2023/11/28
2640
聊聊HttpClient的ConnectionBackoffStrategy
本文主要研究一下HttpClient的ConnectionBackoffStrategy
code4it
2023/10/14
1600
聊聊httpclient的staleConnectionCheckEnabled
本文主要研究一下httpclient的staleConnectionCheckEnabled
code4it
2023/11/24
4680
聊聊RestTemplate对HttpClient的适配
org/springframework/http/client/ClientHttpRequestFactory.java
code4it
2023/10/09
4580
聊聊RestTemplate对HttpClient的适配
httpclient参数配置
默认的话,是从response里头读timeout参数的,没有读到则设置为-1,这个代表无穷,这样设置是有点问题了,如果是https链接的话,则可能会经常报
code4it
2018/09/17
7.1K0
聊聊HttpClient的ServiceUnavailableRetryStrategy
本文主要研究一下HttpClient的ServiceUnavailableRetryStrategy
code4it
2023/10/13
3180
聊聊httpclient的connect
org/apache/http/conn/HttpClientConnectionOperator.java
code4it
2023/11/26
3900
聊聊skywalking的httpclient-plugin
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/resources/skywalking-plugin.def
code4it
2020/03/09
1.1K0
聊聊skywalking的httpclient-plugin
Android使用HttpClient方法和易错问题
HttpClient为Android开发人员提供了跟简洁的操作Http网络连接的方法,在连接过程中也有两种方式,get和post,先看一下怎样实现的
全栈程序员站长
2021/12/23
3050
相关推荐
再聊HttpClient的NoHttpResponseException
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档