- Notifications
You must be signed in to change notification settings - Fork9.2k
Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
License
square/okhttp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
See theproject website for documentation and APIs.
HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTPefficiently makes your stuff load faster and saves bandwidth.
OkHttp is an HTTP client that’s efficient by default:
- HTTP/2 support allows all requests to the same host to share a socket.
- Connection pooling reduces request latency (if HTTP/2 isn’t available).
- Transparent GZIP shrinks download sizes.
- Response caching avoids the network completely for repeat requests.
OkHttp perseveres when the network is troublesome: it will silently recover from common connectionproblems. If your service has multiple IP addresses, OkHttp will attempt alternate addresses if thefirst connect fails. This is necessary for IPv4+IPv6 and services hosted in redundant datacenters. OkHttp supports modern TLS features (TLS 1.3, ALPN, certificate pinning). It can beconfigured to fall back for broad connectivity.
Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. Itsupports both synchronous blocking calls and async calls with callbacks.
OkHttp follows modern HTTP specifications such as
- HTTP/1.1 -RFC 7231
- HTTP/2 -RFC 9113
- Websockets -RFC 6455
- SSE -Server-sent events
Where the spec is ambiguous, OkHttp follows modern user agents such as popular Browsers or common HTTP Libraries.
OkHttp is principled and avoids being overly configurable, especially when such configuration isto workaround a buggy server, test invalid scenarios or that contradict the relevant RFC.Other HTTP libraries exist that fill that gap allowing extensive customisation including potentiallyinvalid requests.
Example Limitations
- Does not allow GET with a body.
- Cache is not an interface with alternative implementations.
This program downloads a URL and prints its contents as a string.Full source.
OkHttpClientclient =newOkHttpClient();Stringrun(Stringurl)throwsIOException {Requestrequest =newRequest.Builder() .url(url) .build();try (Responseresponse =client.newCall(request).execute()) {returnresponse.body().string(); }}
This program posts data to a service.Full source.
publicstaticfinalMediaTypeJSON =MediaType.get("application/json");OkHttpClientclient =newOkHttpClient();Stringpost(Stringurl,Stringjson)throwsIOException {RequestBodybody =RequestBody.create(json,JSON);Requestrequest =newRequest.Builder() .url(url) .post(body) .build();try (Responseresponse =client.newCall(request).execute()) {returnresponse.body().string(); }}
Further examples are on theOkHttp Recipes page.
OkHttp works on Android 5.0+ (API level 21+) and Java 8+.
OkHttp depends onOkio for high-performance I/O and theKotlin standard library. Both are small libraries with strong backward-compatibility.
We highly recommend you keep OkHttp up-to-date. As with auto-updating web browsers, staying currentwith HTTPS clients is an important defense against potential security problems.Wetrack the dynamic TLS ecosystem and adjust OkHttp to improve connectivity andsecurity.
OkHttp uses your platform's built-in TLS implementation. On Java platforms OkHttp also supportsConscrypt, which integratesBoringSSL with Java. OkHttp will use Conscrypt if it isthe first security provider:
Security.insertProviderAt(Conscrypt.newProvider(),1);
The OkHttp3.12.x
branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lacksupport for TLS 1.2 and should not be used.
Ourchange log has release history.
The latest release is available onMaven Central.
implementation("com.squareup.okhttp3:okhttp:4.12.0")
Snapshot builds areavailable.R8 and ProGuard rules are available.
Also, we have abill of materials (BOM) available to help you keep OkHttp artifacts up to date and be sure about version compatibility.
dependencies {// define a BOM and its version implementation(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))// define any required OkHttp artifacts without version implementation("com.squareup.okhttp3:okhttp") implementation("com.squareup.okhttp3:logging-interceptor") }
OkHttp includes a library for testing HTTP, HTTPS, and HTTP/2 clients.
The latest release is available onMaven Central.
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
MockWebServer is used for firstly for internal testing, and for basic testing of apps using OkHttp client.It is not a full featured HTTP testing library that is developed standalone. It is not being actively developedfor new features. As such you might find your needs outgrow MockWebServer and you may which to use amore full featured testing library such asMockServer.
Building your native images with Graalhttps://www.graalvm.org/ should work automatically.This is not currently in a final released version, so5.0.0-alpha.2
should be used.Please report any bugs or workarounds you find.
See the okcurl module for an example build.
$ ./gradlew okcurl:nativeImage$ ./okcurl/build/graal/okcurl https://httpbin.org/get
Copyright 2019 Square, Inc.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
About
Square’s meticulous HTTP client for the JVM, Android, and GraalVM.