Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

java - Apache HttpClient 4 And JavaScript

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})
        });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...