Class HttpClient

java.lang.Object
dev.gmitch215.bytebox.net.http.HttpClient

public final class HttpClient extends Object
An HTTP client, standing in for 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
  • Method Details

    • newHttpClient

      public static HttpClient newHttpClient()
      Returns a client with the defaults.
      Returns:
      a client with the defaults
    • newBuilder

      public static HttpClient.Builder 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 request
      handler - how to read the body
      Returns:
      the response
      Throws:
      IOException - when the request fails
    • followRedirects

      public HttpClient.Redirect followRedirects()
      Returns how redirects are handled.
      Returns:
      how redirects are handled
    • connectTimeout

      public Optional<Duration> 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

      public HttpClient.Version 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