I’m working with HtmlUnit for the first time and need help with handling multiple redirects to reach a final document.
I have a Crystal Reports server that exposes REST APIs for report generation. The process involves getting a URL from an API endpoint, and when I open this URL in a regular browser, it goes through about three redirects before finally displaying the PDF report.
I’m trying to replicate this browser behavior using HtmlUnit:
try (final WebClient client = new WebClient()) {
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setRedirectEnabled(true);
resultPage = client.getPage(reportUrl);
}
This code gets me to the second redirect but stops there instead of reaching the actual PDF document. What’s the best approach to handle this situation? Should I manually capture the intermediate response and make another call with a fresh WebClient instance, or is there a simpler way to follow all redirects until the final document is reached?