Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsApr 28, 2026·2 min de lecture

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.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires