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ā)送請求則會(huì)讀取不到正常的reponse,因而拋出NoHttpResponseException。大多數(shù)情況下,可以通過重試解決。另外針對因?yàn)閗eep-alive超時(shí)斷開的,可以配置HttpClient的connTimeToLive值小于服務(wù)端的keepAlive值(通常是60s)。
doc
以上就是NoHttpResponseException異常解決優(yōu)化HttpClient配置以避免連接問題的詳細(xì)內(nèi)容,更多關(guān)于HttpClient NoHttpResponseException的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot實(shí)現(xiàn)調(diào)用百度ocr實(shí)現(xiàn)身份識別+二要素校驗(yàn)功能
本文介紹了如何使用Spring Boot調(diào)用百度OCR服務(wù)進(jìn)行身份識別,并通過二要素校驗(yàn)確保信息準(zhǔn)確性,感興趣的朋友一起看看吧2025-03-03
Dubbo擴(kuò)展點(diǎn)SPI實(shí)踐示例解析
這篇文章主要為大家介紹了Dubbo擴(kuò)展點(diǎn)SPI實(shí)踐示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java實(shí)戰(zhàn)員工績效管理系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+Mysql+Maven+HTML實(shí)現(xiàn)一個(gè)員工績效管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01
本文介紹了在不同技術(shù)和框架中配置跨域資源共享(CORS)的方法,包括使用SpringMVC的@CrossOrigin注解、SpringBoot的全局CORS配置、SpringSecurity中的CORS集成以及手動(dòng)設(shè)置響應(yīng)頭,根據(jù)具體需求和技術(shù)棧,選擇合適的方法來確??缬蛘埱蟮陌踩院陀行?/div> 2025-02-02
Spring中的@CrossOrigin注解的使用詳細(xì)解讀
這篇文章主要介紹了Spring中的@CrossOrigin注解的使用詳細(xì)解讀,跨源資源共享(CORS),是由大多數(shù)瀏覽器實(shí)現(xiàn)的W3C規(guī)范,允許對跨域請求進(jìn)行靈活授權(quán),用來代替IFRAME或JSONP等非正規(guī)實(shí)現(xiàn)方式,需要的朋友可以參考下2023-11-11最新評論

