OkHttpClient关闭连接 [英] OkHttpClient close connection
问题描述
我想使用OkHttpClient加载网址,如果给定网址的网站以pdf内容类型响应,我会继续下载PDF,否则我想忽略响应。
I'd like to use OkHttpClient to load a url, and if the website at the given url responds with a pdf content type I will go ahead and download the PDF, otherwise I want to ignore the response.
我的问题是,我是否需要做一些特殊的事情来关闭请求/响应,或者我是否需要做任何事情来表明我不会使用响应如果我选择不读取响应字节流?如果没有,OkHttpClient何时关闭连接?
My question is, do I need to do anything special to close the request/response or do I need to do anything to indicate that i won't be using the response if I choose to not read the response bytestream? If not, when does OkHttpClient close the connection?
推荐答案
调用 response.body()。close()
将会发布响应持有的所有资源。连接池将保持连接打开,但如果它未使用,它将在超时后自动关闭。
Calling response.body().close()
will release all resources held by the response. The connection pool will keep the connection open, but that'll get closed automatically after a timeout if it goes unused.
这篇关于OkHttpClient关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!