NoHttpResponseException異常解決優(yōu)化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,用于表示目標(biāo)服務(wù)器沒有返回一個(gè)正常的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,若該數(shù)據(jù)為空則拋出NoHttpResponseException("The target server failed to respond")
小結(jié)
NoHttpResponseException繼承了IOException,用于表示目標(biāo)服務(wù)器沒有返回一個(gè)正常的http response,一般是目標(biāo)服務(wù)器負(fù)載太高處理不過來因而斷開了連接,也有可能是目標(biāo)服務(wù)器把這個(gè)空閑連接關(guān)閉了,而HttpClient則繼續(xù)用這個(gè)連接發(fā)送請(qǐng)求則會(huì)讀取不到正常的reponse,因而拋出NoHttpResponseException。大多數(shù)情況下,可以通過重試解決。另外針對(duì)因?yàn)閗eep-alive超時(shí)斷開的,可以配置HttpClient的connTimeToLive值小于服務(wù)端的keepAlive值(通常是60s)。
doc
以上就是NoHttpResponseException異常解決優(yōu)化HttpClient配置以避免連接問題的詳細(xì)內(nèi)容,更多關(guān)于HttpClient NoHttpResponseException的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決SpringBoot 測(cè)試類無法自動(dòng)注入@Autowired的問題
這篇文章主要介紹了解決SpringBoot 測(cè)試類無法自動(dòng)注入@Autowired的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Java使用設(shè)計(jì)模式中的代理模式構(gòu)建項(xiàng)目的實(shí)例展示
這篇文章主要介紹了Java使用設(shè)計(jì)模式中的代理模式構(gòu)建項(xiàng)目的實(shí)例展示,代理模式中的代理對(duì)象可以在客戶端和目標(biāo)對(duì)象之間起到中介的作用,需要的朋友可以參考下2016-05-05Idea的Generate Sources無法生成QueryDSL問題及解決方法
這篇文章主要介紹了解決Idea的Generate Sources無法生成QueryDSL問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Java中調(diào)用Python的實(shí)現(xiàn)示例
本文主要介紹了Java中調(diào)用Python的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05如何通過XML方式配置并實(shí)現(xiàn)Mybatis
這篇文章主要介紹了如何通過XML方式配置并實(shí)現(xiàn)Mybatis,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11約定優(yōu)于配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
以前做項(xiàng)目,總是寫Ant配置文件,滿足于自己更靈活的配置,而沒有去思考,這么做到底值不值得2017-08-08