Class HttpClient
java.net.http.HttpClient.
The compiler substitutes java.net.http.HttpClient for this one, so a library that makes a
request the modern way works unchanged. Underneath it is fetch, which counts against the
subrequest limit - 50 per invocation on the free plan, 10,000 on paid - and against the six
simultaneous outgoing connections every plan allows.
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(
HttpRequest.newBuilder(URI.create("https://example.com")).build(),
HttpResponse.BodyHandlers.ofString()
);
send(dev.gmitch215.bytebox.net.http.HttpRequest, dev.gmitch215.bytebox.net.http.HttpResponse.BodyHandler<T>) blocks the way it does on a JVM, which here means suspending the fiber rather than
an OS thread. Nothing would run in parallel with it in any case, because the runtime has one thread.
sendAsync is absent, and for a reason worth naming: it returns a
CompletableFuture, which the class library does not have. A method that cannot work should
not be on the surface, so calling it is a compile error in your own project rather than an
unresolved reference when the program is compiled to WebAssembly.
Async.supply(java.util.function.Supplier<T>) runs work on its own fiber where that is what
was wanted.
Also not here, because the platform has nothing to back it: HTTP/2 negotiation as a choice (the platform decides), a proxy, an executor, a cookie handler, an authenticator, and a client-side certificate. Redirect following is a choice; the rest are the platform's to make.
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAssembles a client.static enumWhat to do with a redirect.static enumWhich HTTP version to use, which on this platform the platform decides. -
Method Summary
Modifier and TypeMethodDescriptionReturns how long a connection may take to establish, empty for no limit.Returns how redirects are handled.static HttpClient.BuilderReturns a builder.static HttpClientReturns a client with the defaults.<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> handler) Sends a request and waits for the answer.version()Returns the version the platform reports, which it chooses rather than the caller.
-
Method Details
-
newHttpClient
Returns a client with the defaults.- Returns:
- a client with the defaults
-
newBuilder
Returns a builder.- Returns:
- a builder
-
send
public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> handler) throws IOException Sends a request and waits for the answer.- Type Parameters:
T- what the body is read as- Parameters:
request- the requesthandler- how to read the body- Returns:
- the response
- Throws:
IOException- when the request fails
-
followRedirects
Returns how redirects are handled.- Returns:
- how redirects are handled
-
connectTimeout
Returns how long a connection may take to establish, empty for no limit.- Returns:
- how long a connection may take to establish, empty for no limit
-
version
Returns the version the platform reports, which it chooses rather than the caller.- Returns:
- the version the platform reports, which it chooses rather than the caller
-