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,用于表示目標服務器沒有返回一個正常的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,若該數據為空則拋出NoHttpResponseException("The target server failed to respond")
小結
NoHttpResponseException繼承了IOException,用于表示目標服務器沒有返回一個正常的http response,一般是目標服務器負載太高處理不過來因而斷開了連接,也有可能是目標服務器把這個空閑連接關閉了,而HttpClient則繼續(xù)用這個連接發(fā)送請求則會讀取不到正常的reponse,因而拋出NoHttpResponseException。大多數情況下,可以通過重試解決。另外針對因為keep-alive超時斷開的,可以配置HttpClient的connTimeToLive值小于服務端的keepAlive值(通常是60s)。
doc
以上就是NoHttpResponseException異常解決優(yōu)化HttpClient配置以避免連接問題的詳細內容,更多關于HttpClient NoHttpResponseException的資料請關注腳本之家其它相關文章!
相關文章
解決SpringBoot 測試類無法自動注入@Autowired的問題
這篇文章主要介紹了解決SpringBoot 測試類無法自動注入@Autowired的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Idea的Generate Sources無法生成QueryDSL問題及解決方法
這篇文章主要介紹了解決Idea的Generate Sources無法生成QueryDSL問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02