OkHttpClient关闭连接 [英] OkHttpClient close connection

查看:4251
本文介绍了OkHttpClient关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆