Skip to content
An HTTP client for Android, Kotlin, and Java.
Java Kotlin Other
Branch: master
Clone or download
swankjesse Merge pull request #5396 from thescouser89/documentation
Update README.md to match example code for POST
Latest commit e0e191b Aug 25, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildscript Configure gradle artifact uploads Mar 17, 2019
.circleci Bump CI JDK versions (#5386) Aug 21, 2019
.github Basic Gradle support Mar 17, 2019
android-test Test Android session reuse Aug 11, 2019
docs Merge pull request #5362 from square/jwilson.0813.releasing Aug 21, 2019
fuzzing Move tests from okhttp-tests/src/test/java to okhttp/src/test/java Mar 26, 2019
gradle/wrapper Gradle Wrapper 5.5 (#5271) Jul 6, 2019
mockwebserver Prepare for release 4.1.0. Aug 12, 2019
okcurl Remove the application plugin from okcurl Aug 12, 2019
okhttp-brotli Brotli Interceptor docs Aug 23, 2019
okhttp-dnsoverhttps Prepare for release 4.1.0. Aug 12, 2019
okhttp-hpacktests Convert Hpack to Kotlin (#4987) Apr 28, 2019
okhttp-logging-interceptor Ensure overridden content length/type headers are logged Aug 17, 2019
okhttp-sse Prepare for release 4.1.0. Aug 12, 2019
okhttp-testing-support OpenJSSE Platform support (#5369) Aug 18, 2019
okhttp-tls Prepare for release 4.1.0. Aug 12, 2019
okhttp-urlconnection Prepare for release 4.1.0. Aug 12, 2019
okhttp OpenJSSE Platform support (#5369) Aug 18, 2019
samples Add proxy event logging (#5300) Jul 25, 2019
.gitignore Move the API docs from /okhttp/api to /okhttp/4.x Jun 22, 2019
.gitmodules Add hpack-test-case test module. Jul 6, 2014
.travis.yml Avoid gradle assemble in Travis (#5312) Jul 29, 2019
BUG-BOUNTY.md Includes this repo in our open source bug bounty program. May 11, 2015
CHANGELOG.md Prepare for release 4.1.0. Aug 12, 2019
CONTRIBUTING.md Improve OkHttp documentation. Jun 21, 2019
LICENSE.txt Initial import. Jul 23, 2012
README.md Update README.md to match example code for POST Aug 24, 2019
build.gradle Kotlin 1.3.50 Aug 23, 2019
checkstyle.xml Basic Gradle support Mar 17, 2019
deploy_website.sh Revise the upgrading guide for OkHttp 4.x. Jun 23, 2019
gradle.properties Prepare next development version. Aug 12, 2019
gradlew Gradle Wrapper 5.5 (#5271) Jul 6, 2019
gradlew.bat Gradle Wrapper 5.5 (#5271) Jul 6, 2019
mkdocs.yml Merge pull request #5235 from square/jwilson.0624.coc Jun 25, 2019
settings.gradle Extended Android tests (#5298) Aug 10, 2019

README.md

OkHttp

See the project website for documentation and APIs.

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently 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 connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp supports modern TLS features (TLS 1.3, ALPN, certificate pinning). It can be configured to fall back for broad connectivity.

Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.

Get a URL

This program downloads a URL and prints its contents as a string. Full source.

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

Post to a Server

This program posts data to a service. Full source.

public static final MediaType JSON
    = MediaType.get("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(json, JSON);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

Further examples are on the OkHttp Recipes page.

Requirements

OkHttp works on Android 5.0+ (API level 21+) and on Java 8+.

OkHttp depends on Okio for high-performance I/O and the Kotlin standard library. Both are small libraries with strong backwards-compatibility.

We highly recommend you keep OkHttp up-to-date. As with auto-updating web browsers, staying current with HTTPS clients is an important defense against potential security problems. We track the dynamic TLS ecosystem and adjust OkHttp to improve connectivity and security.

OkHttp uses your platform's built-in TLS implementation. On Java platforms OkHttp also supports Conscrypt, which integrates BoringSSL with Java. OkHttp will use Conscrypt if it is the first security provider:

Security.insertProviderAt(Conscrypt.newProvider(), 1);

The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lack support for TLS 1.2 and should not be used. But because upgrading is difficult we will backport critical fixes to the 3.12.x branch through December 31, 2020.

Releases

Our change log has release history.

implementation("com.squareup.okhttp3:okhttp:4.1.0")

Snapshot builds are available.

R8 / ProGuard

If you are using R8 or ProGuard add the options from okhttp3.pro.

You might also need rules for Okio which is a dependency of this library.

MockWebServer

OkHttp includes a library for testing HTTP, HTTPS, and HTTP/2 clients.

testImplementation("com.squareup.okhttp3:mockwebserver:4.1.0")

License

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.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.
You can’t perform that action at this time.