HttpRequest () { HttpRequest.Builder builder = HttpRequest.newBuilder(). setHeader("User-Agent", myUserAgent). timeout(Duration.ofSeconds(10)). uri(URI.create(myUrl)); if ("HEAD".equals(myMethod)) { builder.method(myMethod, HttpRequest.BodyPublishers.noBody()); } else if ("POST".equals(myMethod)) { if (myContent == null || myContent.isBlank()) { throw new EmptyHttpRequestBody(); } builder.setHeader("Chunked", Boolean.toString(false)); builder.setHeader("Content-Type", String.format(Locale.ENGLISH, "%s; charset=%s", myContentType, myCharset)); builder.setHeader("Content-Encoding", "gzip"); builder.POST(HttpRequest.BodyPublishers.ofByteArray(getCompressedContent())); } else if ("GET".equals(myMethod)) { builder.GET(); } else { throw new IllegalHttpRequestTypeException(); } myExtraHeaders .forEach((k, v) -> builder.setHeader(k,v)); return builder.build(); }