Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class WebClient

  1. Object
  2. WebClient

An outbound HTTP client with its own configuration: a client certificate for mutual TLS, an HTTP/2 requirement, timeouts, and a pool of connections that belongs to it alone.

The static methods on Web are a client with every default and no pool. Create one of these when a request needs more than that:

WebClient bank = new WebClient()
        .setClientCertificate(ClientCertificate.pkcs12(p12, password))
        .setRequireHttp2(true)
        .setRequestTimeoutMillis(10000);
Web.Result r = bank.getJson("https://api.bank.example/v1/balance", token);

Create it once and share it – it is safe to use from every handler at once – because the pool is what makes a second request to the same host skip the TCP and TLS handshakes. close() releases the pool.

The configuration is fixed by the first request. A setter called after one throws IllegalStateException. Changing the credential of a client whose pool already holds connections authenticated as somebody else is exactly how two identities end up sharing a connection, so it is not possible; make a second client instead. Two clients never share a connection, whatever their configuration.

Certificate and hostname verification are always on, as they are for Web: there is no option to turn them off, and a client certificate does not change how the server’s certificate is checked.

HTTP/2. With setRequireHttp2(boolean) the request is made over HTTP/2 or not at all: a peer that does not speak it fails the request with an IOException saying so, and the request is not sent to it as HTTP/1.1. Without it the packaged binary offers HTTP/2 over TLS and falls back to HTTP/1.1 when the server declines; Web.Result.getProtocol() says which was used. The local Java SE runtime makes an ordinary request with HttpURLConnection, which speaks HTTP/1.1 only, and a client that REQUIRES HTTP/2 with the JDK’s java.net.http client (Java 11 or newer), so APNs and other HTTP/2-only services work in the development loop and in tests. There, open(String, String, List, long) – a streamed exchange – is still HTTP/1.1 only and refuses such a client.

Constructors

public WebClient()

Methods

public synchronized WebClient setRequireHttp2(boolean requireHttp2)Requires HTTP/2: a peer that does not negotiate it fails the request.
public synchronized boolean isRequireHttp2()
public synchronized WebClient setClientCertificate(ClientCertificate clientCertificate)The certificate presented when a server asks for one, or null for none.
public synchronized ClientCertificate getClientCertificate()
public synchronized WebClient setConnectTimeoutMillis(int millis)How long establishing the connection – TCP and TLS – may take.
public synchronized int getConnectTimeoutMillis()
public synchronized WebClient setReadTimeoutMillis(int millis)How long the transfer may make NO progress before it is abandoned.
public synchronized int getReadTimeoutMillis()
public synchronized WebClient setRequestTimeoutMillis(int millis)The ceiling on a whole request, connection to last byte.
public synchronized int getRequestTimeoutMillis()
public synchronized WebClient setReuseConnections(boolean reuseConnections)Whether a finished request’s connection is kept for the next request to the same server.
public synchronized boolean isReuseConnections()
public Web.Result get(String url) throws IOException
public Web.Result getJson(String url, String bearerToken) throws IOException
public Web.Result postJson(String url, String json, String bearerToken) throws IOException
public Web.Result request(String method, String url, List headers, byte[] body) throws IOExceptionAs [Web#request(String, String, List, byte[])], through this client.
public Web.Exchange open(String method, String url, List headers, long requestLength) throws IOExceptionAs Web.open(String, String, List, long), through this client: its certificate, pool and connect and read timeouts.
public void close()Releases the client’s pooled connections.

Inherited methods

Constructor details

WebClient

public WebClient()

Method details

setRequireHttp2

public synchronized WebClient setRequireHttp2(boolean requireHttp2)
Requires HTTP/2: a peer that does not negotiate it fails the request. Default false.

isRequireHttp2

public synchronized boolean isRequireHttp2()

setClientCertificate

public synchronized WebClient setClientCertificate(ClientCertificate clientCertificate)
The certificate presented when a server asks for one, or null for none.

getClientCertificate

public synchronized ClientCertificate getClientCertificate()

setConnectTimeoutMillis

public synchronized WebClient setConnectTimeoutMillis(int millis)
How long establishing the connection – TCP and TLS – may take. Default 30000; 0 means no limit.

getConnectTimeoutMillis

public synchronized int getConnectTimeoutMillis()

setReadTimeoutMillis

public synchronized WebClient setReadTimeoutMillis(int millis)

How long the transfer may make NO progress before it is abandoned. Default 30000; 0 means no limit. A transfer that keeps moving is never cut off by this, however long it takes – that is the request timeout.

The packaged binary measures this in whole seconds (libcurl’s low-speed limit), so a value is rounded UP to the next second there.

getReadTimeoutMillis

public synchronized int getReadTimeoutMillis()

setRequestTimeoutMillis

public synchronized WebClient setRequestTimeoutMillis(int millis)
The ceiling on a whole request, connection to last byte. Default 0, no ceiling. A request still running when it is reached fails with an IOException.

getRequestTimeoutMillis

public synchronized int getRequestTimeoutMillis()

setReuseConnections

public synchronized WebClient setReuseConnections(boolean reuseConnections)
Whether a finished request’s connection is kept for the next request to the same server. Default true. With false every request opens, and closes, a connection of its own.

isReuseConnections

public synchronized boolean isReuseConnections()

get

public Web.Result get(String url) throws IOException

getJson

public Web.Result getJson(String url, String bearerToken) throws IOException

postJson

public Web.Result postJson(String url, String json, String bearerToken) throws IOException

request

public Web.Result request(String method, String url, List headers, byte[] body) throws IOException
As [Web#request(String, String, List, byte[])], through this client.

open

public Web.Exchange open(String method, String url, List headers, long requestLength) throws IOException
As Web.open(String, String, List, long), through this client: its certificate, pool and connect and read timeouts. The exchange counts as a request in flight until it is closed, so close waits for it.

close

public void close()
Releases the client’s pooled connections. A request made after this fails; one already running finishes first, and the last of them releases the connections.