Apache HttpClient 4 And JavaScript

断了今生、忘了曾经 提交于 2019-12-17 16:39:42

问题


I Use Apache HttpClient to Access a webpage . i want to click on a link but the link is javaScript , how can i process click on a javascript link and follow url redirection ?

sample javascript and html code :

<a href="javascript:send(32023, 'YGHN_JKM', '8LMK');"> link</a>


function send(content_id, fic, cgRate) {
        var params = new Hash();
        params.set('content_id', content_id);
        params.set('tool', fic);
        params.set('cgRate', cgRate);

        new Ajax.Updater('return', '/mypkg/tools', {
            method: 'post',
            parameters: params,
            evalScripts: true,
            onInitialize: new Effect.Appear('loader', {duration: 0.0}),
            onComplete: new Effect.Fade('loader', {duration: 1.2})
        });
}

回答1:


It looks like the page you are trying to get to uses AJAX to fetch the link. It would be difficult to emulate this behavior with just vanilla HTTPClient. HTTPClient is for "raw" HTTP communication, and not browser emulation. Instead, I recommend using something akin to HtmlUnit, which can emulate a browser and execute the JavaScript on the page: http://htmlunit.sourceforge.net/

HtmlUnit has rather good JavaScript support, but it is not perfect. If you need to rely on 100% perfect browser emulation, you need to use a browser automation framework such as Selenium: http://seleniumhq.org/



来源:https://stackoverflow.com/questions/7260282/apache-httpclient-4-and-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!