Apache HttpClient 4 和 JavaScript
我使用 Apache HttpClient 访问网页。我想单击一个链接,但该链接是 JavaScript,我如何处理单击 JavaScript 链接并遵循 url 重定向?
示例 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})
});
}
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})
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试访问的页面似乎使用 AJAX 来获取链接。仅使用普通的 HTTPClient 来模拟这种行为是很困难的。 HTTPClient 用于“原始”HTTP 通信,而不是浏览器模拟。相反,我建议使用类似于 HtmlUnit 的东西,它可以模拟浏览器并在页面上执行 JavaScript: http://htmlunit.sourceforge.net/
HtmlUnit 对 JavaScript 有相当好的支持,但并不完美。如果需要依赖100%完美的浏览器模拟,则需要使用浏览器自动化框架,例如Selenium:http://seleniumhq.org/
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/