11package com .wlanboy .javahttpclient .client ;
22
3+ import java .io .IOException ;
34import java .io .PrintWriter ;
45import java .io .StringWriter ;
56import java .net .InetAddress ;
@@ -35,13 +36,19 @@ public class ClientService {
3536 private static final int MAX_REDIRECTS = 10 ;
3637
3738 private final HttpClient client ;
39+ private final HttpClient clientHttp11 ;
3840
3941 public ClientService () {
4042 client = HttpClient .newBuilder ()
4143 .version (Version .HTTP_2 )
4244 .followRedirects (Redirect .NEVER )
4345 .connectTimeout (Duration .ofSeconds (10 ))
4446 .build ();
47+ clientHttp11 = HttpClient .newBuilder ()
48+ .version (Version .HTTP_1_1 )
49+ .followRedirects (Redirect .NEVER )
50+ .connectTimeout (Duration .ofSeconds (10 ))
51+ .build ();
4552 }
4653
4754 public ResponseEntity <String > sendRequest (JavaHttpRequest requestData , HttpHeaders incomingHeaders ) {
@@ -54,10 +61,24 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
5461 // DNS-Auflösung vor dem Request (diagnostisch)
5562 String resolvedIps = resolveDns (currentUri .getHost ());
5663
57- // Erster Request mit allen Headern
58- HttpResponse <String > response = client .send (
59- buildRequest (currentUri , currentMethod , currentBody , requestData , incomingHeaders ),
60- BodyHandlers .ofString ());
64+ // Erster Request mit HTTP/2, Fallback auf HTTP/1.1 bei Protocol-Fehler
65+ record RequestResult (HttpClient activeClient , HttpResponse <String > response , boolean usedFallback ) {}
66+ RequestResult initial ;
67+ try {
68+ initial = new RequestResult (client ,
69+ client .send (buildRequest (currentUri , currentMethod , currentBody , requestData , incomingHeaders ),
70+ BodyHandlers .ofString ()),
71+ false );
72+ } catch (IOException e ) {
73+ logger .warn ("HTTP/2 fehlgeschlagen ({}), Fallback auf HTTP/1.1" , e .getMessage ());
74+ initial = new RequestResult (clientHttp11 ,
75+ clientHttp11 .send (buildRequest (currentUri , currentMethod , currentBody , requestData , incomingHeaders ),
76+ BodyHandlers .ofString ()),
77+ true );
78+ }
79+ HttpClient activeClient = initial .activeClient ();
80+ HttpResponse <String > response = initial .response ();
81+ boolean usedFallback = initial .usedFallback ();
6182
6283 // Redirects manuell verfolgen
6384 while (REDIRECT_CODES .contains (response .statusCode ()) && redirectChain .size () < MAX_REDIRECTS ) {
@@ -81,7 +102,7 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
81102 }
82103
83104 // Folge-Requests ohne originale Browser-Header (kein Auth-Leak)
84- response = client .send (
105+ response = activeClient .send (
85106 buildRequest (currentUri , currentMethod , currentBody , null , null ),
86107 BodyHandlers .ofString ());
87108 }
@@ -94,6 +115,7 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
94115 .headers (h -> {
95116 h .addAll (responseHeaders );
96117 h .set ("X-Protocol-Version" , protocolVersion );
118+ if (usedFallback ) h .set ("X-Protocol-Fallback" , "HTTP/1.1" );
97119 if (resolvedIps != null ) h .set ("X-Resolved-IP" , resolvedIps );
98120 if (!redirectChain .isEmpty ()) {
99121 h .set ("X-Redirect-Chain" , serializeChain (redirectChain ));
0 commit comments