Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class WebClient
- Object
- 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
Inherited methods
Constructor details
WebClient
public WebClient()Method details
setRequireHttp2
public synchronized WebClient setRequireHttp2(boolean requireHttp2)isRequireHttp2
public synchronized boolean isRequireHttp2()setClientCertificate
public synchronized WebClient setClientCertificate(ClientCertificate clientCertificate)getClientCertificate
public synchronized ClientCertificate getClientCertificate()setConnectTimeoutMillis
public synchronized WebClient setConnectTimeoutMillis(int millis)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)getRequestTimeoutMillis
public synchronized int getRequestTimeoutMillis()setReuseConnections
public synchronized WebClient setReuseConnections(boolean reuseConnections)isReuseConnections
public synchronized boolean isReuseConnections()get
public Web.Result get(String url)
throws IOExceptionThrows
getJson
public Web.Result getJson(String url, String bearerToken)
throws IOExceptionThrows
postJson
public Web.Result postJson(String url, String json, String bearerToken)
throws IOExceptionThrows
request
public Web.Result request(String method, String url, List headers, byte[] body)
throws IOExceptionThrows
open
public Web.Exchange open(String method, String url, List headers, long requestLength)
throws IOExceptionWeb.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.Throws
close
public void close()