Java: HTTP Header ohne Content?

1 Antwort

Von Experte alfredo153 bestätigt

Deine Lösung basiert schon auf einem HEAD-Request. Das heißt, der Response Body darf im Response nicht enthalten sein.

Aus RFC 7231:

The HEAD method is identical to GET except that the server MUST NOT send a message body in the response (i.e., the response terminates at the end of the header section). The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request had been a GET, except that the payload header fields ( Section 3.3) MAY be omitted.

Was du nun noch machen könntest, um die Performance zu verbessern:

a) Nutze die neue HttpClient API (>= JDK 11). Sie sollte besser optimiert sein, u.a. da sie standardmäßig HTTP/2 verwendet. Nur wenn es nicht unterstützt wird, erfolgt der Fallback auf HTTP/1.1 (die von HttpURLConnection unterstützte Protokollversion).

Hier ein Beispiel:

HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
  .uri(URI.create("YOUR URL ..."))
  .method("HEAD", HttpRequest.BodyPublishers.noBody())
  .build();
HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
HttpHeaders headers = response.headers();

Optional<String> contentLength = headers.firstValue​("Content-Length");

Zwei Exceptions (IOException, InterruptedException) müssten hierbei noch aufgefangen werden.

b) Diesen Prozess könntest du auch asynchron gestalten oder du verlagerst Anfragen auf verschiedene Threads. Teste es einfach einmal.

c) Du kannst zudem Timeouts einbauen. Schau dir die beiden Code-Beispiele aus der API Referenz an.

Was du unabhängig von alldem allerdings beachten solltest: Der Response Header muss Content-Length nicht zwingend enthalten. Dazu zitiert aus RFC 7540:

A request or response that includes a payload body can include a content-length header field.

oder aus RFC 2616:

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET. (...) Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

Die dort angesprochene Sektion 4.4 enthält noch weitere Informationen, wie sich die Länge des Body auch ohne angegebene Content-Length ermitteln lässt.

J0hannes2015 
Fragesteller
 06.03.2021, 14:50

Hallo, erstmal danke für ihre Antwort, sie hat mich sehr weit voran gebracht (Also wirklich sehr weit, der installer läuft schon perfekt (jedoch leider nur wenn er in eclipse gestartet wird)). Eine Frage die etwas vom Thema abweicht: Der http Client ist ja erst mit Java 9 draußen, jedoch haben vieler meiner Freunde einschließlich mich (ausgenommen der IDE) Java 8. Gibt es eine ähnliche Lösung, die trotzdem so effizient ist oder gibt es eine Möglichkeit den http Client auch in einer jre 8 zu benutzen?
LG Johannes

0