java OkHttpClient 关闭连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29055724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OkHttpClient close connection
提问by Wise Shepherd
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 加载一个 url,如果给定 url 的网站以 pdf 内容类型响应,我将继续下载 PDF,否则我想忽略响应。
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?
我的问题是,如果我选择不读取响应字节流,我是否需要做任何特殊的事情来关闭请求/响应,或者我是否需要做任何事情来表明我不会使用响应?如果没有,OkHttpClient 什么时候关闭连接?
回答by Jesse Wilson
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.
调用response.body().close()
将释放响应持有的所有资源。连接池将保持连接打开,但如果它未使用,它会在超时后自动关闭。