Skip to content

Cloudflare protection. (TypeError: Cannot find function observe.) #1136

Description

@FurfurDev

Hello, I'm trying to download a jar file from the website www.spigotmc.org , but I'm having trouble getting through Cloudflare's protection

My code:

import org.htmlunit.CookieManager;
import org.htmlunit.NicelyResynchronizingAjaxController;
import org.htmlunit.WebClient;
import org.htmlunit.html.HtmlPage;
import org.junit.jupiter.api.Test;

public class NetworksUtilTest
{
    private static final WebClient webClient = new WebClient();

    @Test
    void htmlUnitTest() throws Exception
    {
        webClient.addRequestHeader("Connection", "keep-alive");
        webClient.addRequestHeader("Upgrade-Insecure-Requests", "1");
        webClient.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
        webClient.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        webClient.addRequestHeader("Accept-Language", "en-US,en;q=0.9");
        webClient.addRequestHeader("Accept-Encoding", "gzip, deflate");

        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setFetchPolyfillEnabled(true);
        webClient.getOptions().setPopupBlockerEnabled(true);
	    webClient.getOptions().setGeolocationEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setDoNotTrackEnabled(true);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setDownloadImages(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setScreenHeight(768);
        webClient.getOptions().setScreenWidth(1366);
        webClient.getOptions().setCssEnabled(false);
	    webClient.getOptions().setTimeout(5000);

        CookieManager cookieManager = new CookieManager();
        webClient.setCookieManager(cookieManager);

        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.setAlertHandler((page, message) -> System.err.println("[alert] " + message)); // to be changed
        webClient.setJavaScriptTimeout(5000);
        webClient.getCookieManager().setCookiesEnabled(true);
        webClient.getCache().setMaxSize(1);

        HtmlPage htmlCloudflarePage = webClient.getPage("https://www.spigotmc.org/resources/vault.34315/download?version=344916");
        webClient.waitForBackgroundJavaScript(5_000);
        System.out.println(htmlCloudflarePage.getWebResponse().getResponseHeaderValue("Content-Type"));

        webClient.close();
    }
}

logs: log.txt

Gradle Version: gradle-9.5.1
HtmlUnit Version: 5.1.0

Update (10.08.2026)

XML Response: cloudflare.xml

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions