Skip to content
Square’s meticulous HTTP client for Java and Kotlin.
Java Kotlin Shell
Branch: master
Clone or download
monkey-mas Refactor OkHttpClient#xxxTimeout(...) to fix errorprone PreferJavaTim…
…eOverload in tests (#5797)

* Fix errorprone PreferJavaTimeOverload in tests

When compiling OkHttp, we have the warnings regarding PreferJavaTimeOverload.

For example:
```
okhttp/okhttp/src/test/java/okhttp3/EventListenerTest.java:193: warning: [PreferJavaTimeOverload] Prefer using java.time-based APIs when available. Note that this checker does not and cannot guarantee that the overloads have equivalent semantics, but that is generally the case with overloaded methods.
        .readTimeout(250, TimeUnit.MILLISECONDS)
                    ^
    (see https://errorprone.info/bugpattern/PreferJavaTimeOverload)
```

To fix this, `java.time.Duration` API is used instead of `java.util.concurrent.TimeUnit` in tests.

* refactor OkHttpClient timeout() methods
Latest commit 733a58a Feb 20, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildscript Bump Travis to JDK 11 (#5601) Nov 17, 2019
.circleci Merge pull request #5779 from yschimke/circleci_up Feb 11, 2020
.github Official Gradle Wrapper Validation GitHub Action Jan 15, 2020
android-test Merge pull request #5703 from yschimke/cleaner_fix Jan 11, 2020
docs Prepare for release 4.4.0. Feb 17, 2020
fuzzing Move tests from okhttp-tests/src/test/java to okhttp/src/test/java Mar 26, 2019
gradle/wrapper Gradle 6.1 Jan 19, 2020
mockwebserver Prepare for release 4.4.0. Feb 17, 2020
okcurl Switch to Gradle's Maven Publishing Plugin Jan 20, 2020
okhttp-bom Publish a BOM for OkHttp 4.x. Jan 21, 2020
okhttp-brotli Prepare for release 4.4.0. Feb 17, 2020
okhttp-dnsoverhttps Prepare for release 4.4.0. Feb 17, 2020
okhttp-hpacktests ErrorProne cleanup of two modules (#5640) Dec 9, 2019
okhttp-logging-interceptor Prepare for release 4.4.0. Feb 17, 2020
okhttp-sse Prepare for release 4.4.0. Feb 17, 2020
okhttp-testing-support Add a canceled event to EventListener Feb 16, 2020
okhttp-tls Prepare for release 4.4.0. Feb 17, 2020
okhttp-urlconnection Prepare for release 4.4.0. Feb 17, 2020
okhttp Refactor OkHttpClient#xxxTimeout(...) to fix errorprone PreferJavaTim… Feb 20, 2020
samples Add a canceled event to EventListener Feb 16, 2020
.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 Bump Travis to JDK 11 (#5601) Nov 17, 2019
BUG-BOUNTY.md Updated bug bounty (#5753) Jan 25, 2020
CHANGELOG.md Prepare for release 4.4.0. Feb 17, 2020
CONTRIBUTING.md Document debug logging. Dec 31, 2019
LICENSE.txt Initial import. Jul 23, 2012
README.md Prepare for release 4.4.0. Feb 17, 2020
build.gradle Prepare next development version. Feb 17, 2020
deploy_website.sh Include Brotli docs in the OkHttp website Oct 6, 2019
gradle.properties Switch to Gradle's Maven Publishing Plugin Jan 20, 2020
gradlew Gradle 6.0.1 Nov 27, 2019
gradlew.bat Gradle Wrapper 5.5 (#5271) Jul 6, 2019
mkdocs.yml Security Providers Documentation Jan 13, 2020
settings.gradle Publish a BOM for OkHttp 4.x. Jan 21, 2020

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

The latest release is available on Maven Central.

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

Snapshot builds are available. R8 and ProGuard rules are available.

MockWebServer

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

The latest release is available on Maven Central.

testImplementation("com.squareup.okhttp3:mockwebserver:4.4.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.