美文网首页
HttpClient 杂记

HttpClient 杂记

作者: jeff_zhang_js | 来源:发表于2018-08-24 20:58 被阅读0次

    HttpUriRequest#abort()

    被 HttpClient 执行的 HTTP 请求可以在执行的任意阶段通过调用HttpUriRequest#abort()方法而中止。这个方法是线程安全的,而且可以从任意线程中调用


    连接参数:

    'http.socket.timeout':定义了套接字的毫秒级超时时间(SO_TIMEOUT),这就是等待数据,换句话说,在两个连续的数据包之间最大的闲置时间。

    'http.tcp.nodelay':决定了是否使用Nagle算法。Nagle算法视图通过最小化发送的分组数量来节省带宽。当应用程序希望降低网络延迟并 高性能时,它们可以关闭Nagle 算法。

    'http.socket.buffer-size':决定了内部套接字缓冲使用的大小,来缓冲数据同时接收/传输 HTTP 报文。如果这个参数没有被设置,那么 HttpClient 将会分配 8192 字节的套接字缓存。

    'http.socket.linger':使用指定的秒数拖延时间来设置SO_LINGER。最大的连接超时值是平台指定的。值0暗示了这个选项是关闭的。值-1暗示了使用了JRE默认的。这个设置仅仅影响套接字关闭操作。如果这个参数没有被设置,那么就假设值为-1(JRE 默认)。

    'http.connection.timeout':决定了直到连接建立时的毫秒级超时时间。超时时间的值为 0 解释为一个无限大的时间。如果这个参数没有被设置,连接操作将不会超时(无限大的超时时间)

    'http.connection.stalecheck':决定了是否使用旧的连接检查。当在一个连接之上执行一个请求而服务器端的连接已经关闭时,关闭旧的连接检查可能导致在获得一个I/O 错误风险时显著的性能提升(对于每一个请求,检查时间可以达到 30 毫秒)。出于性能的关键操作,检查应该被关闭。如果这个参数没有被设置,那么旧的连接将会在每个请求执行之前执行。

    ‘http.connection.max-line-length':决定了最大请求行长度的限制。如果设置为一个正数,任何 HTTP 请求行超过这个限制将会引发 java.io.IOException 异常。负数或零将会关闭这个检查。如果这个参数没有被设置,那么就不强制进行限制了。

    'http.connection.max-header-count':决定了允许的最大HTTP头部信息数量。如果设置为一个正数,从数据流中获得的 HTTP 头部信息数量超过这个限制就会引发java.io.IOException 异常。负数或零将会关闭这个检查。如果这个参数没有被设置,那么就不强制进行限制了。

     'http.connection.max-status-line-garbage':决定了在期望得到HTTP响应状态行之前可忽略请求行的最大数量。使用 HTTP/1.1 持久性连接,这个问题产生的破碎的脚本将会返回一个错误的Content-Length(有比指定的字节更多的发送)。不幸的是,在某些情况下,这个不能在错误响应后来侦测,只能在下一次之前。所以HttpClient 必须以这种方式跳过那些多余的行。0 是不允许在状态行之前的所有垃圾/空行。如果这个参数没有被设置那就假设是不限制的。


    Ensuring release of low level resources

    The difference between closing the content stream and closing the response is that the former will attempt to keep the underlying connection alive by consuming the entity content while the latter immediately shuts down and discards the connection.

    Please note that the HttpEntity#writeTo(OutputStream) method is also required to ensure proper release of system resources once the entity has been fully written out. If this method obtains an instance of java.io.InputStream by calling HttpEntity#getContent(), it is also expected to close the stream in a finally clause.

    When working with streaming entities, one can use the EntityUtils#consume(HttpEntity) method to ensure that the entity content has been fully consumed and the underlying stream has been closed.


    1.2.2. HttpClient resource deallocation

    When an instance CloseableHttpClient is no longer needed and is about to go out of scope the connection manager associated with it must be shut down by calling the CloseableHttpClient#close() method.

    中止请求   被 HttpClient 执行的 HTTP 请求可以在执行的任意阶段通过调用HttpUriRequest#abort()方法而中止



    Connection keep alive strategy:链接 keep-alive策略

    相关文章

      网友评论

          本文标题:HttpClient 杂记

          本文链接:https://www.haomeiwen.com/subject/mxyiiftx.html