okhttp类图2
2018-03-30 17:38:20 0 举报
一张图带你了解okhttp框架
作者其他创作
大纲/内容
Request
+HttpUrl url;+String method;+Headers headers;+RequestBody body;+Object tag;
+ 。。。一些请求信息包括请求类型get or post 请求头、请求body。。
用于构建web应用时使用,它可以在浏览器和服务器之间提供基于tcp链接的双向通道
ConnectionPool
涉及到同步,较复杂
c.RealCall通过RealInterceptorChain循环调用RetryAndFollowUpInterceptor、ConnectInterceptor、CallServerInterceptor的process(),最后由CallServerInterceptor的process()调用Http1Codec进行调用okIO库,进行网络请求
RetryAndFollowUpInterceptor
+ OkHttpClient client;+StreamAllocation streamAllocation;
+ operation1(params):returnType- operation2(params)- operation3()
StreamAllocation
通过trustRootIndex获取出所有证书,并验证他们的有效性
Interceptor
+ Response intercept(Chain chain)
Dispatcher
-ExecutorService executorService;-Deque readyAsyncCalls-Deque runningAsyncCalls-Deque runningSyncCalls
#synchronized void enqueue(AsyncCall call) 异步#synchronized void executed(RealCall call)同步+synchronized void cancelAll()
拦截器,用于对请求、响应、和链接进行拦截
ConnectInterceptor
OkHttpClient
+ Internal.instance+ List DEFAULT_PROTOCOLS+List DEFAULT_CONNECTION_SPECS#Dispatcher dispatcher;#Proxy proxy;#List protocols;#List connectionSpecs;#List interceptors;#List networkInterceptors;#ProxySelector proxySelector;系统类#CookieJar cookieJar;#Cache cache;#InternalCache internalCache;#SocketFactory socketFactory;系统类用于创建socket#SSLSocketFactory sslSocketFactory;系统类用于创建SSLSocket#CertificateChainCleaner certificateChainCleaner;#Authenticator proxyAuthenticator;#Authenticator authenticator;#ConnectionPool connectionPool;#Dns dns;
+Builder sslSocketFactory(SSLSocketFactory sslSocketFactory) +Call newCall(Request request)
MediaType
+ 。。。
给Cache类提供了对缓存内容的增删改查功能
Call
Request request()Response execute()void enqueue(Callback responseCallback)void cancel()boolean isExecuted();boolean isCanceled();Call clone();
RealInterceptorChain
CookieJar
AndroidPlatform
-Class sslParametersClass;-OptionalMethod setUseSessionTickets;-OptionalMethod setHostname;
AndroidTrustRootIndex
+X509Certificate findByIssuerAndSignature(X509Certificate cert)
Jdk9Platform
a.调用时,new一个OkHttpClient对象,再new一个Request对象,然后配合调用OKhttpClient的newCall方法,获得一个Call的子类对象RealCall,在执行Call的execute()或者enqueue()进行网络请求
根据域名查询ip
包含了子类AndroidTrustRootIndex和子类BasicTrustRootIndex,用户根据某证书查询相同作者的相关证书调用get()方法返回new AndroidTrustRootIndex对象
Cloneable
系统克隆接口
调度器,拥有一个线程池和一些线程配置策略,用于执行网络请求Call的子类对象
Factory
newWebSocket()
JdkWithJettyBootPlatform
CacheControl
+。。。
+。。。缓存控制策略
TrustRootIndex
#X509Certificate findByIssuerAndSignature(X509Certificate cert);+TrustRootIndex get(X509TrustManager trustManager)+TrustRootIndex get(X509Certificate... caCerts)
Cache
# InternalCache internalCache = new InternalCache()#DiskLruCache cache;
Authenticator
该子类的trustManager()被调用,返回对应的Manager,因为每个平台中获取X509TrustManager的对应类不同
Internal
+ Internal+ addLenient+setCache+Socket deduplicate();+。。。
Protocol
DiskLruCache
很复杂
很多方法
Dns
+ List lookup(String hostname)
okHttp升级版API
根据response返回一个带权限认证的request
Flushable
+ void flush() throws IOException;提供输出到stream的功能
https 链接策略组
BasicCertificateChainCleaner
-TrustRootIndex trustRootIndex;
Headers
+ 。。。http请求头信息
Platform
+ Platform PLATFORM = findPlatform();
+ findPlatform()创建时自动返回匹配Android、jdk9、jdkWithJettyBoot的平台,见其3个子类+X509TrustManager trustManager(SSLSocketFactory sslSocketFactory)+CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager trustManager)返回BasicCertificateChainCleaner对象
Call newCall(Request request);
CallServerInterceptor
从response中提取保存cookie,给request添加cookie
InternalCache
X509TrustManager
不同平台有不同的实现
最近最少使用原则的文件缓存操作实现类,通过线程池实现
Proxy
-Type type;-SocketAddress sa;
...
将对应平台的X509TrustManager对象传入TrustRootIndex的具体子类,得到AndroidTrustRootIndex对象在将其传入BasicCertificateChainCleaner得到CertificateChainCleaner的子类对象
通过反射,执行其X509TrustManager的具体对象的findTrustAnchorByIssuerAndSignature()返回一个X509Certificate对象
RealCall
+ OkHttpClient client;+RetryAndFollowUpInterceptor retryAndFollowUpInterceptor;+Request originalRequest;
+ Response execute()+void enqueue(Callback responseCallback) +Response getResponseWithInterceptorChain()
HttpUrl
+ 。。。构建一个请求URL
RequestBody
+ 。。。标记content的类型、内容、size
代理类,包含了代理策略(无代理、http、代理、socket代理)和一个socket地址
Chain
+ Request request();+Response proceed(Request request) +Connection connection();
ConnectionSpec
-CipherSuite[] APPROVED_CIPHER_SUITES;+ConnectionSpec MODERN_TLS;+ConnectionSpec COMPATIBLE_TLS;+ConnectionSpec CLEARTEXT;
CertificateChainCleaner
通过OkHttpClient的sslSocketFactory()对它的certificateChainCleaner属性赋值
连接池类,复用http、http2链接,减少网络延迟
b.调用者执行execute()或者enqueue()时最终都会调用getResponseWithInterceptorChain()获取请求结果,该方法中调用了RetryAndFollowUpInterceptor、BridgeInterceptor、CacheInterceptor、ConnectInterceptor、CallServerInterceptor
okHttp3类图
证书清理者,过滤出授信任的证书,调用get()通过Platform获取X509TrustManager,根据Manager对象,获得CertificateChainCleaner对象
WebSocket
request()send()close()cancel()
0 条评论
下一页