Apache HttpClient 4 和 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7260282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache HttpClient 4 And JavaScript
提问by JaVaBoy
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 ?
我使用 Apache HttpClient 访问网页。我想单击一个链接,但该链接是 javaScript,我如何处理单击 javascript 链接并遵循 url 重定向?
sample javascript and html code :
示例 javascript 和 html 代码:
<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})
});
}
回答by Lauri Piispanen
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/
看起来您尝试访问的页面使用 AJAX 来获取链接。仅使用普通的 HTTPClient 很难模拟这种行为。HTTPClient 用于“原始”HTTP 通信,而不是浏览器模拟。相反,我建议使用类似于 HtmlUnit 的东西,它可以模拟浏览器并在页面上执行 JavaScript: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/
HtmlUnit 对 JavaScript 的支持相当好,但并不完美。如果需要100%完美的浏览器仿真,则需要使用Selenium等浏览器自动化框架:http: //seleniumhq.org/