lysine-dev /okhttp

A meticulous HTTP client for the JVM, Android, and GraalVM.

★ 47,109⑂ 9,287👁 1,628⑃ 5,310● 4,333▤ 0◈ 0
KotlinApache-2.0Created 2012-07-23lysine.dev/okhttp/
47,109
stars
+2
9,287
forks
1,628
watchers
5,310
prs
4,333
issues
0
releases
0
discussions

README

OkHttp
======

See the [project website][okhttp] 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.

A well behaved user agent
-------------------------

OkHttp follows modern HTTP specifications such as

 HTTP Semantics - RFC 9110
 HTTP Caching- RFC 9111
 HTTP/1.1 - RFC 9112
 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 is
to 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 potentially
invalid requests.

Example Limitations

 Does not allow GET with a body.
 Cache is not an interface with alternative implementations.

Get a URL
---------

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

java
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][post_example].

java
public static final MediaType JSON = MediaType.get("application/json");

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][recipes].


Requirements
------------

OkHttp works on Android 5.0+ (API level 21+)

Top issues

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON