如何使用Delphi和Indy跟踪URL重定向? [英] How to track URL redirects using Delphi and Indy?
问题描述
我收到一些带有url链接的营销电子邮件,这些链接从一个站点重定向到另一个站点。我想编写一个程序来跟踪使用Delphi和Indy进行的每个URL重定向。我想遍历每个URL,记录完整的QueryString和在此过程中可能设置的任何Cookie。
I get several marketing emails with url links that get redirected from site to site to site. I'd like to write a program to track each URL redirect using Delphi and Indy. I'd like to traverse each URL, record the full QueryString and any Cookies that may have been set during the process.
如何使用Indy组件D2010附带?
How do I do this using the Indy components that come with D2010?
推荐答案
首先,您需要一个HTTP客户端,即 TIdHTTP
First of all you need a HTTP client, which is TIdHTTP
in Indy.
现在,您将需要一个保存结果的数据结构:
Now you will need a data structure that will hold your results:
TRedirection = record
queryString: String;
cookies: TStrings;
end;
TRedirectionArray = array of TRedirection;
创建一个可以完成工作的类(需要一个类,因为事件函数定义为对象过程
):
Create a class that does the work (a class is required, because the event functions are defined as procedure of object
):
TRedirectionTester = class
private
FRedirData: TRedirectionArray;
procedure redirectEvent(Sender: TObject; var dest: string;
var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
procedure newCookie(ASender: TObject; ACookie: TIdCookie; var VAccept: Boolean);
public
function traverseURL(url: String): TRedirectionArray;
property RedirData: TRedirectionArray read FRedirData;
end;
这提供了基本功能-您可以调用 traverseURL
,然后返回一个 TRedirectionArray
,其中包含查询字符串和cookie。
This provides basic functionality - you can call traverseURL
with an URL, and it will return a TRedirectionArray
with the querystrings and cookies involved.
然后实施 OnRedirect
事件:
procedure TRedirectionTester.redirectEvent(Sender: TObject; var dest: string;
var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
var
redirDataLength: Integer;
begin
Handled := True;
redirDataLength := Length(FRedirData);
SetLength(FRedirData, redirDataLength + 1);
FRedirData[redirDataLength].queryString := dest;
FRedirData[redirDataLength].cookies := TStringList.Create;
end;
这将在数组中添加一个条目,并存储重定向的查询字符串。由于此重定向本身不包含cookie(在请求重定向页面时设置了cookie),因此您还不能在此处添加任何cookie。
This will add an entry in the array, and store the querystring of the redirection. As this redirection itself doesn't contain a cookie (cookies are set when requesting the redirected page), you can't add any cookies here yet.
这就是为什么需要一个 OnNewCookie
处理程序:
That's why you will need an OnNewCookie
handler:
procedure TRedirectionTester.newCookie(ASender: TObject; ACookie: TIdCookie; var VAccept: Boolean);
var
redirDataLength: Integer;
begin
VAccept := True;
redirDataLength := High(FRedirData);
if (Assigned(FRedirData[redirDataLength].cookies)) then
FRedirData[redirDataLength].cookies.Add(ACookie.CookieText);
end;
除了将 CookieText
添加到数据集。该字段包含Cookie的摘要,它是请求页面时发送的实际字符串数据。
This does nothing but adding the CookieText
to the data set. That field contains a 'summary' of the cookie - it's the actual string data that is sent when requesting a page.
最后,通过实现<$ c来将其放在一起$ c> traverseURL 函数:
function TRedirectionTester.traverseURL(url: String): TRedirectionArray;
var
traverser: TIdHTTP;
begin
traverser := TIdHTTP.Create();
traverser.HandleRedirects := True;
traverser.OnRedirect := redirectEvent;
traverser.CookieManager := TIdCookieManager.Create();
traverser.CookieManager.OnNewCookie := newCookie;
SetLength(FRedirData, 1);
FRedirData[0].queryString := url;
FRedirData[0].cookies := TStringList.Create;
traverser.Get(url);
Result := FRedirData;
end;
它没有做很多事情:它创建所需的对象,并分配事件处理程序。然后,它将第一个网址添加为第一个重定向(即使它不是真正的重定向,我还是为了完整性而添加了它)。
调用 Get
然后发送请求。它将在最终页面找到并由网络服务器返回后返回。
It doesn't do much: It creates the required objects, and assigns the event handlers. Then it adds the first url as the first redirection (even though it's not a real redirection, I added it for completeness).
The call to Get
then sends the requests. It will return after the final page is located and returned by the webserver.
我用 http://bit.ly/Lb2Vho 。
但这仅处理由HTTP状态码301或302引起的重定向据我所知,它不处理通过< meta>
标记或javascript完成的重定向。
要添加该功能,您必须检查对 Get
的调用结果,并对其进行解析以搜索此类重定向。
This however only handles redirects that are caused by an HTTP status code 301 or 302. As far as I know it doesn't handle redirects that are done via <meta>
tags or javascript.
To add that functionality, you have to check the results of the call to Get
, and parse that to search for such redirects.
这篇关于如何使用Delphi和Indy跟踪URL重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!