Try cookie-based retry first, fall back to response body

Attempt the cookie+UA retry before using FlareSolverr's response body
so that when cookies work, subsequent requests can reuse them without
going through FlareSolverr. Only fall back to the response body when
the retry is still CF-protected.
This commit is contained in:
Etheirystech 2026-04-09 16:36:05 +02:00
parent 714d7980b4
commit f449ad6682

View file

@ -71,16 +71,22 @@ public override HttpResponse PostResponse(HttpResponse response)
InjectCookies(newRequest, result);
// Use FlareSolverr's response body directly when available.
// When CF selectively challenges by IP/UA, FlareSolverr may not
// receive a challenge and thus returns no cf_clearance cookie.
// A second request without that cookie gets 403'd.
// Try cookie-based retry first so subsequent requests can
// reuse the cookies without going through FlareSolverr.
var finalResponse = _httpClient.Execute(newRequest);
if (!CloudFlareDetectionService.IsCloudflareProtected(finalResponse))
{
return finalResponse;
}
// Cookie retry was blocked — fall back to FlareSolverr's response body.
// This happens when CF selectively challenges by IP/UA and FlareSolverr
// doesn't receive a challenge, so no cf_clearance cookie is returned.
if (result.Solution.Response.IsNotNullOrWhiteSpace())
{
var headers = new HttpHeader();
// Preserve the Content-Type from FlareSolverr's solution so downstream
// parsers (e.g. JSON indexers) interpret the body correctly.
if (result.Solution.Headers?.ContentType.IsNotNullOrWhiteSpace() == true)
{
headers.ContentType = result.Solution.Headers.ContentType;
@ -95,9 +101,6 @@ public override HttpResponse PostResponse(HttpResponse response)
HttpStatusCode.OK);
}
// Fallback: if FlareSolverr returned no body, retry with cookies (original behavior)
var finalResponse = _httpClient.Execute(newRequest);
return finalResponse;
}