问题
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