Configs2026年4月28日·1 分钟阅读

OkHttp — Modern HTTP Client for Java and Kotlin

A reliable and efficient HTTP client for the JVM and Android with connection pooling, transparent GZIP, response caching, and WebSocket support.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Stage only
信任
信任等级:Established
入口
OkHttp Overview
安全暂存命令
npx -y tokrepo@latest install ee9438ae-433e-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

OkHttp is an HTTP and HTTP/2 client by Square for Java and Kotlin applications. It handles connection pooling, request retries, and response caching transparently so developers can focus on application logic rather than networking boilerplate.

What OkHttp Does

  • Sends synchronous and asynchronous HTTP requests with a clean builder API
  • Maintains a connection pool that reuses sockets across requests to reduce latency
  • Supports HTTP/2 multiplexing and transparent GZIP compression
  • Provides interceptors for logging, authentication, retries, and custom request modification
  • Handles WebSocket connections for real-time bidirectional communication

Architecture Overview

OkHttp is built around a dispatcher that manages a thread pool for async calls. Requests pass through a chain of interceptors (application-level then network-level) before reaching the connection layer. The connection pool keeps idle sockets alive for reuse, and a disk-based cache stores responses following HTTP caching semantics.

Self-Hosting & Configuration

  • Add via Gradle: implementation("com.squareup.okhttp3:okhttp:4.12.0")
  • Configure timeouts with OkHttpClient.Builder (connectTimeout, readTimeout, writeTimeout)
  • Add interceptors for logging via okhttp3-logging-interceptor module
  • Set up certificate pinning with CertificatePinner for enhanced security
  • Customize the connection pool size and keep-alive duration for high-throughput scenarios

Key Features

  • Transparent connection pooling reduces request latency by reusing TCP connections
  • Interceptor chain allows modular request/response processing
  • HTTP/2 support with graceful fallback to HTTP/1.1
  • Built-in response caching that respects Cache-Control headers
  • First-class Kotlin coroutine support via the suspending extension

Comparison with Similar Tools

  • Java HttpClient (JDK 11+) — built-in with no extra dependency but lacks interceptors and connection pool tuning
  • Apache HttpClient — feature-rich but heavier API surface and slower to adopt modern protocols
  • Retrofit — a higher-level REST client that uses OkHttp as its transport layer
  • Ktor Client — Kotlin-native async client; better for pure Kotlin multiplatform projects

FAQ

Q: Does OkHttp work on Android? A: Yes. OkHttp is the default HTTP engine on Android and is used internally by the platform.

Q: How do I add logging? A: Add the okhttp3-logging-interceptor dependency and attach an HttpLoggingInterceptor to your client builder.

Q: Can I use OkHttp with coroutines? A: Yes. Use the Call.enqueue suspend extension or wrap calls with withContext(Dispatchers.IO).

Q: What is the minimum Java version? A: OkHttp 4.x requires Java 8+ or Android API 21+.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产