Salesforce 不使用 HTMLUnit

Salesforce not working with HTMLUnit

我正在尝试使用 HTMLUnit 在 Salesforce 上进行一些网络抓取,以获取组织许可信息。如果我尝试通过常规 login/test url 访问 Salesforce,它会起作用。但我希望能够使用 /secur/frontdoor.jsp?sid= 方法通过会话 ID 登录。

当我尝试使用它时,Salesforce 抱怨 javascript 未启用。但是我在 HTMLUnit 中启用了它。

   java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);

        final WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
        HtmlPage page;

        webClient.waitForBackgroundJavaScript(10000);
        webClient.waitForBackgroundJavaScriptStartingBefore(10000);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setAppletEnabled(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setActiveXNative(true);
        webClient.getOptions().setAppletEnabled(true);

        page = webClient.getPage("https://salesforce--domain/secur/frontdoor.jsp?sid=SessionId");

想通了。出于某种原因,您不会自动重定向。所以你只需要从第一个getPage获取URL然后去新的newURL.

     java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);

    final WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
    HtmlPage page;

    webClient.waitForBackgroundJavaScript(10000);
    webClient.waitForBackgroundJavaScriptStartingBefore(10000);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setAppletEnabled(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setActiveXNative(true);
    webClient.getOptions().setAppletEnabled(true);

    page = webClient.getPage("https://salesforce--domain/secur/frontdoor.jsp?sid=SessionId");

页数=webClient.getPage(page.getURL());