Class Web.Exchange
- Enclosing class:
Web
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.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Ends the exchange.The first value of every response header, by lower-case name.Every response header, by lower-case name, each a List of values in arrival order -- Set-Cookie and the like keep every line.getHeaderValues(String name) "HTTP/1.1" or "HTTP/2", as negotiated.Where the request body goes.The response body as it arrives.intThe response status, waiting for the response headers.
-
Method Details
-
getRequestBody
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.- Throws:
IOException
-
getStatus
The response status, waiting for the response headers. Ends a request body that is still open first.- Throws:
IOException
-
getProtocol
"HTTP/1.1" or "HTTP/2", as negotiated.- Throws:
IOException
-
getHeaders
The first value of every response header, by lower-case name.- Throws:
IOException
-
getHeaderValueMap
Every response header, by lower-case name, each a List of values in arrival order -- Set-Cookie and the like keep every line.- Throws:
IOException
-
getHeader
- Throws:
IOException
-
getHeaderValues
- Throws:
IOException
-
getResponseBody
The response body as it arrives. Reading it to the end completes the exchange;close()before that cancels it.- Throws:
IOException
-
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.
-