Android OkHttp 源码浅析二

OkHttp 配置参数:

 @get:JvmName("dispatcher") val dispatcher: Dispatcher = builder.dispatcher

  @get:JvmName("connectionPool") val connectionPool: ConnectionPool = builder.connectionPool

  /**
   * Returns an immutable list of interceptors that observe the full span of each call: from before
   * the connection is established (if any) until after the response source is selected (either the
   * origin server, cache, or both).
   */
  @get:JvmName("interceptors") val interceptors: List<Interceptor> =
      builder.interceptors.toImmutableList()

  /**
   * Returns an immutable list of interceptors that observe a single network request and response.
   * These interceptors must call [Interceptor.Chain.proceed] exactly once: it is an error for
   * a network interceptor to short-circuit or repeat a network request.
   */
  @get:JvmName("networkInterceptors") val networkInterceptors: List<Interceptor> =
      builder.networkInterceptors.toImmutableList()

  @get:JvmName("eventListenerFactory") val eventListenerFactory: EventListener.Factory =
      builder.eventListenerFactory

  @get:JvmName("retryOnConnectionFailure") val retryOnConnectionFailure: Boolean =
      builder.retryOnConnectionFailure

  @get:JvmName("authenticator") val authenticator: Authenticator = builder.authenticator

  @get:JvmName("followRedirects") val followRedirects: Boolean = builder.followRedirects

  @get:JvmName("followSslRedirects") val followSslRedirects: Boolean = builder.followSslRedirects

  @get:JvmName("cookieJar") val cookieJar: CookieJar = builder.cookieJar

  @get:JvmName("cache") val cache: Cache? = builder.cache

  @get:JvmName("dns") val dns: Dns = builder.dns

  @get:JvmName("proxy") val proxy: Proxy? = builder.proxy

  @get:JvmName("proxySelector") val proxySelector: ProxySelector =
      when {
        // Defer calls to ProxySelector.getDefault() because it can throw a SecurityException.
        builder.proxy != null -> NullProxySelector
        else -> builder.proxySelector ?: ProxySelector.getDefault() ?: NullProxySelector
      }

  @get:JvmName("proxyAuthenticator") val proxyAuthenticator: Authenticator =
      builder.proxyAuthenticator

  @get:JvmName("socketFactory") val socketFactory: SocketFactory = builder.socketFactory

  private val sslSocketFactoryOrNull: SSLSocketFactory?

  @get:JvmName("sslSocketFactory") val sslSocketFactory: SSLSocketFactory
    get() = sslSocketFactoryOrNull ?: throw IllegalStateException("CLEARTEXT-only client")

  @get:JvmName("x509TrustManager") val x509TrustManager: X509TrustManager?

  @get:JvmName("connectionSpecs") val connectionSpecs: List<ConnectionSpec> =
      builder.connectionSpecs

  @get:JvmName("protocols") val protocols: List<Protocol> = builder.protocols

  @get:JvmName("hostnameVerifier") val hostnameVerifier: HostnameVerifier = builder.hostnameVerifier

  @get:JvmName("certificatePinner") val certificatePinner: CertificatePinner

  @get:JvmName("certificateChainCleaner") val certificateChainCleaner: CertificateChainCleaner?

  /**
   * Default call timeout (in milliseconds). By default there is no timeout for complete calls, but
   * there is for the connect, write, and read actions within a call.
   */
  @get:JvmName("callTimeoutMillis") val callTimeoutMillis: Int = builder.callTimeout

  /** Default connect timeout (in milliseconds). The default is 10 seconds. */
  @get:JvmName("connectTimeoutMillis") val connectTimeoutMillis: Int = builder.connectTimeout

  /** Default read timeout (in milliseconds). The default is 10 seconds. */
  @get:JvmName("readTimeoutMillis") val readTimeoutMillis: Int = builder.readTimeout

  /** Default write timeout (in milliseconds). The default is 10 seconds. */
  @get:JvmName("writeTimeoutMillis") val writeTimeoutMillis: Int = builder.writeTimeout

  /** Web socket and HTTP/2 ping interval (in milliseconds). By default pings are not sent. */
  @get:JvmName("pingIntervalMillis") val pingIntervalMillis: Int = builder.pingInterval

  /**
   * Minimum outbound web socket message size (in bytes) that will be compressed.
   * The default is 1024 bytes.
   */
  @get:JvmName("minWebSocketMessageToCompress")

dispatcher 用于线程调度

connectionPool 连接池  64 个or 5 host 可以提升复用性 方便管理和提升性能

interceptors 

networkInterceptors

eventListenerFactory 事件监听器 连接建立 发送head body 等

retryOnConnectionFailure 连接 / 请求 失败是否重置

authenticator 自动认证修正 比如 401 无权限访问等 可以用于刷新token 

followRedirects 是否继续重定向 

followSslRedirects 在followRedirects 开启时 协议切换时 是否继续重定向  比如http 切换https 默认true 

cookieJar 存储器 存储cookie 默认是空实现
cache 本地缓存
dns 域名系统 域名解析为ip 函数为 lookup getAllByName List inntAddress 通过域名返回IPList---InetAddress.getAllByName(hostName)
proxy 代理 转发  代理服务器 代理多种方式 还有反向代理 类型 DIRECT 直连 HTTP SOCKS,默认NO_PROXY
proxySelector 判断是否代理 通过select 方法 返回List<Proxy> 
proxyAuthenticator 如果代理不可用 需要授权 需要验证机制
socketFactory http请求本质是java socket 通过socketFactory  创建
sslSocketFactory ssl 请求, tls ssl 加密 连接
x509TrustManager 证书验证器,通过证书和签名进行验证 ,X509 证书格式
connectionSpecs List 连接标准 规范,https 连接 客户端 发送tls 连接 的规范 比如连接套件 非对称加密等,ConnectionSpec 类定义各种规范比如 RESTRICTED_TLS支持TLS 1.3 1.2 ,MODERN_TLS1.3 1.2 ,COMPATIBLE_TLS 支持 1.3  1.2 1.1 1.0,CLEARTEXT 明文 传输https 报文 不用tls
protocols List, 支持协力的版本号 比如 1.0 , 1.1, SPDY2,SPDY3 ,HTTP2 最好使用加密 h2c ,H2_PRIOR 非加密,
hostnameVerifier: 验证合法性 域名 只验证第一个 [0] 
certificatePinner:加强验证,可以配置证书 hash 
certificateChainCleaner:=== 证书验证相关 操作X509
callTimeoutMillis 超时
connectTimeoutMillis 连接超时
readTimeoutMillis 读取超时
writeTimeoutMillis 写入超时
pingIntervalMillis 心跳机制  websocket / http2 使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值