square/okhttp


An HTTP client for Android, Kotlin, and Java.

http://square.github.io/okhttp/

License: Apache-2.0

Language: Java

Keywords: android, java, kotlin


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 has one library dependency on Okio, a small library for high-performance I/O.

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:3.14.2")

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:3.14.2")

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.

Project Statistics

Sourcerank 22
Repository Size 25.5 MB
Stars 33,078
Forks 7,264
Watchers 1,648
Open issues 203
Dependencies 0
Contributors 200
Tags 68
Created
Last updated
Last pushed

Top Contributors See all

Jesse Wilson Jake Wharton Adrian Cole Yuri Schimke Amir Livneh nfuller Dave Roberge Marcelo Cortes Benoît Quenaudon Kafka Eric Cochran Shaishav Gandhi Egor Andreevich Tobias T monkey-mas Patrick Forhan Narayan Kamath lingmingyb Eric Denman Zac Sweers

Packages Referencing this Repo

com.squareup.okhttp:mockwebserver
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
eu.the4thfloor.mockwebserver:com.squareup.okhttp.mockwebserver
An HTTP client for Android, Kotlin, and Java.
This package is no longer available on Maven
com.squareup.okhttp:okhttp-protocols
An HTTP client for Android, Kotlin, and Java.
Latest release 1.3.0 - Updated - 33.1K stars
com.squareup.okhttp:okhttp-apache
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp:okhttp-tests
An HTTP client for Android, Kotlin, and Java.
Latest release 1.6.0 - Updated - 33.1K stars
com.squareup.okhttp.sample:static-server
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp:okhttp
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp3:okhttp
null
Latest release 4.0.0-alpha02 - Updated - 33.1K stars
com.squareup.okhttp.sample:sample-parent
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp3:okhttp-tls
null
Latest release 4.0.0-alpha02 - Updated - 33.1K stars
com.squareup.okhttp3:okhttp-urlconnection
null
Latest release 4.0.0-alpha02 - Updated - 33.1K stars
Karamunting.Square.OkHttp3.UrlConnection
OkHttp URLConnection
Latest release 3.14.2 - Updated - 33.1K stars
Karamunting.Android.Square.OkHttp
An HTTP & HTTP/2 client for Android and Java applications
Latest release 3.14.2 - Updated - 33.1K stars
com.squareup.okhttp3:okhttp-testing-support
An HTTP client for Android, Kotlin, and Java.
Latest release 3.13.1 - Updated - 33.1K stars
com.squareup.okhttp3:mockwebserver
An HTTP client for Android, Kotlin, and Java.
Latest release 3.13.1 - Updated - 33.1K stars
com.squareup.okhttp3:logging-interceptor
An HTTP client for Android, Kotlin, and Java.
Latest release 3.13.1 - Updated - 33.1K stars
Karamunting.Square.OkHttp3.LoggingInterceptor
OkHttp Logging Interceptor
Latest release 3.14.2 - Updated - 33.1K stars
com.squareup.okhttp:okhttp-testing-support
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp:okhttp-ws
An HTTP client for Android, Kotlin, and Java.
Latest release 2.7.5 - Updated - 33.1K stars
com.squareup.okhttp3.sample:simple-client
An HTTP client for Android, Kotlin, and Java.
Latest release 3.10.0 - Updated - 33.1K stars

Recent Tags See all

parent-4.0.0-RC3 June 24, 2019
parent-4.0.0-RC2 June 21, 2019
parent-4.0.0-RC1 June 04, 2019
parent-4.0.0-alpha02 May 26, 2019
parent-3.14.2 May 19, 2019
parent-4.0.0-ALPHA01 May 09, 2019
parent-3.12.3 May 07, 2019
parent-3.14.1 April 10, 2019
parent-3.12.2 March 14, 2019
parent-3.14.0 March 14, 2019
parent-3.13.1 February 05, 2019
parent-3.13.0 February 05, 2019
parent-3.12.1 December 23, 2018
parent-3.12.0 November 17, 2018
parent-3.11.0 July 13, 2018

Interesting Forks See all

dunwen/okhttp
An HTTP+HTTP/2 client for Android and Java applications.然后现在我添加了中文注释了。
Java - Last pushed - 10 stars
Nextpeer/okhttp
An HTTP+SPDY client for Android and Java applications (Java 1.6 support).
Java - Apache-2.0 - Last pushed - 7 stars - 4 forks
ruifeng2357/okhttp
An HTTP+HTTP/2 client for Android and Java applications.
Java - Updated - 5 stars
bytedance/okhttp
An HTTP+HTTP/2 client for Android and Java applications.
Java - Last pushed - 4 stars - 2 forks
airbnb/okhttp
An HTTP+SPDY client for Android and Java applications.
Java - Apache-2.0 - Last pushed - 4 stars - 3 forks

Something wrong with this page? Make a suggestion

Last synced: 2019-06-25 15:39:58 UTC

Login to resync this repository