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

public static final class Web.Exchange

  1. Object
  2. Web.Exchange

One streamed HTTP exchange: the request body written as it is produced, the response read as it arrives, neither held in memory whole. The way to move a large upload or download, or to forward one (see HttpProxy).

Use it from one thread, in order: write and close getRequestBody when the request has one, then getStatus and the headers, then read getResponseBody. Writes block until the transport has taken the bytes and reads until some have arrived, so a slow peer at either end slows the producer rather than filling memory – each direction buffers a fixed 256 KiB window. close ends the exchange whatever state it is in; before the response has been read to its end that CANCELS it, closing the upstream connection rather than draining it. Always close it, in a finally.

Redirects are never followed here: a streamed body cannot be replayed. The request timeout of a WebClient does not apply either – a stream may legitimately run for as long as it moves – while its connect and read timeouts do, the read timeout bounding each wait for the peer. A stream has a connection of its own rather than one from the client’s pool.

Methods

public OutputStream getRequestBody() throws IOExceptionWhere the request body goes.
public int getStatus() throws IOExceptionThe response status, waiting for the response headers.
public String getProtocol() throws IOException“HTTP/1.1” or “HTTP/2”, as negotiated.
public Map getHeaders() throws IOExceptionThe first value of every response header, by lower-case name.
public Map getHeaderValueMap() throws IOExceptionEvery response header, by lower-case name, each a List of values in arrival order – Set-Cookie and the like keep every line.
public String getHeader(String name) throws IOException
public List getHeaderValues(String name) throws IOException
public InputStream getResponseBody() throws IOExceptionThe response body as it arrives.
public void close()Ends the exchange.

Inherited methods

Method details

getRequestBody

public OutputStream getRequestBody() throws IOException
Where the request body goes. Closing it ends the request body; the stream is the exchange’s and closing it does not end the exchange.

getStatus

public int getStatus() throws IOException
The response status, waiting for the response headers. Ends a request body that is still open first.

getProtocol

public String getProtocol() throws IOException
“HTTP/1.1” or “HTTP/2”, as negotiated.

getHeaders

public Map getHeaders() throws IOException
The first value of every response header, by lower-case name.

getHeaderValueMap

public Map getHeaderValueMap() throws IOException
Every response header, by lower-case name, each a List of values in arrival order – Set-Cookie and the like keep every line.

getHeader

public String getHeader(String name) throws IOException

getHeaderValues

public List getHeaderValues(String name) throws IOException

getResponseBody

public InputStream getResponseBody() throws IOException
The response body as it arrives. Reading it to the end completes the exchange; close before that cancels it.

close

public void close()
Ends the exchange. Before the response body has been read to its end this cancels it: the transfer is aborted and its connection closed.