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

