【问题标题】:Detect client side redirect when crawling with Delphi TidHttp使用 Delphi TidHttp 爬行时检测客户端重定向
【发布时间】:2019-12-06 08:54:26
【问题描述】:

我是一名 Delphi 开发人员,负责为面向公众的网站抓取 <title>meta 描述和关键字。

这很好,直到我遇到一个自我重定向并且没有使用 HTTP 302/301 重定向的网站。

例如,如果我在 url 中输入 example.com,它将自动跳转到 example-b.com - 但在客户端 - 不是通过 HTTP 301 或 302。

我的目标是获取example-b.com 的标题、描述和关键字。

如果有帮助,我会在 delphi 中使用 TidHttp。

编辑
我试过这个answer,但据说它只适用于 HTTP 301 和 302。我已经处理了这些重定向。我试图弄清楚如何处理<meta>referh 标记或其他执行重定向的html 命令。

编辑 2
刚刚找到这个命令:

<meta http-equiv="refresh" content="5;url=http://thisinterestsme.com/detecting-ajax-requests-with-php/">
header( "refresh:5;url=http://thisinterestsme.com/php-forcing-https-over-http/" );
header('Location: http://thisinterestsme.com/php-forcing-https-over-http/');
window.location.href= 'http://thisinterestsme.com/php-forcing-https-over-http/';

如果我错过了其他命令,请告诉我。

【问题讨论】:

标签: php html delphi


【解决方案1】:

TIdHTTP 遵循meta refresh 重定向,即使HandleRedirects 设置为True。但是,如果hoNoParseMetaHTTPEquiv 不包含在TIdHTTP 的属性HTTPOptions 中,它会解析&lt;meta http-equiv=...。默认情况下不包括该选项。执行请求后,您可以通过IdHTTP.MetaHTTPEquiv 访问解析值,这是IdHTTP.Response.MetaHTTPEquiv 的简写。

由于 Indy 不处理它,您必须自己承担从值中解析 URL、执行重定向和检测循环/无限重定向的所有负担。 Refresh 标头也是如此,它不是 official standards 的一部分。

Location 标头仅对 HTTP 状态代码 201 Created202 Accepted3xx 有效。它应该只在状态码为3xx304 Not Modified 除外)时重定向,Indy 已经这样做了,因此您不需要以任何特殊方式处理此问题。

最后,支持 JavaScript 重定向使任务变得更加复杂,TIdHTTP 将无法破解。这似乎是headless browser 的用例。

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2020-01-24
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多