Ну вот, код... попробовал просто сделать поддержку http и html редиректов, но какой-то баг мешает. Если стравить методу download страничку без redirect, то страничка скачивается без проблем. Если же с ним - то "400 Bad Request", хотя в трассировке четко видно как мы заходим в функции определение этого redirect'a, и что самое главное - он определяется верно...
Код: Выделить всё
{$mode ObjFpc}
uses httpsend,classes,synautil;
type
TStatus=(HtmlRedirection,HttpRedirection,LoginPage,RequiredPage,DownloadError);
type
TDownloadPage=class(THttpSend)
protected
fNickName:string;
fPassWordValue:string;
fStatus:TStatus;
DocumentStrings:TStringList;
public
Constructor Create;
function Download(URL:string):boolean;
//function Login(URL:string):boolean;
Destructor Destroy; override;
published
property NickName:string read fNickName write fNickName;
property PassWord:string read fPassWordValue write fPassWordValue;
property Status:TStatus read fStatus;
public
NickField,PassField:string;//пока не используется
URL,redirect:String;
function AbsoluteUrl:string;
//function ParseForLoginFields:boolean;
function IsHttpRedirection:boolean;//<---проверяем присутствует ли Http Redirect, если да - сохраняем его в redirect
function IsHtmlRedirection:boolean;//<--- тоже самое только для Html Redirect
end;
constructor TDownloadPage.Create;
begin
inherited;
fNickName:='';
fPassWordValue:='';
NickField:='';
PassField:='';
URL:='';
DocumentStrings:=TStringList.create;
end;
function TDownloadPage.Download(URL:string):boolean;//<---проблемный метод
begin
result:=false;
fStatus:=DownloadError;
if not(HttpMethod('GET',URL)) then
exit;
DocumentStrings.LoadFromStream(Document);
if (IsHttpRedirection) or (IsHtmlRedirection) then begin//<---если есть Redirection
{ document.clear;
DocumentStrings.clear;
headers.clear; }
result:=Download(redirect);//<---скачиваем другую страницу
exit;
end;
result:=true;
fStatus:=RequiredPage;
end;
function TDownloadPage.AbsoluteUrl:string;
var res:String;
begin
if pos('http://',Url)=0 then
Url:=TargetHost+Url;
end;
function TDownloadPage.IsHttpRedirection:boolean;
var i:integer;
begin
writeln('Http redirection');
result:=false;
for i:=0 to Headers.Count-1 do
if pos('Location',Headers.Strings[i])<>0 then//<---если в ответе сервера есть Location хидер
if SeparateRight(Headers.Strings[i],':')<>AbsoluteUrl then begin//<---и он не равен запрашиваемому URL, значит нас редиректнули
result:=true;
fStatus:=HttpRedirection;
redirect:=SeparateRight(Headers.Strings[i],': ');
writeln('Redirect is ',redirect);
end;
end;
function TDownloadPage.IsHtmlRedirection:boolean;
var ind:integer;
begin
writeln('Html redirection');
result:=false;
if pos('<meta http-equiv="refresh"',DocumentStrings.Text)<>0 then//тут идет парсинг файла на мета тег.
begin
result:=true;
fStatus:=HtmlRedirection;
redirect:=GetBetween('URL=','"',DocumentStrings.Text);
writeln('Redirect is ',redirect);
end;
end;
destructor TDownloadPage.Destroy;
begin
DocumentStrings.free;
inherited;
end;
var Page:TDownloadPage;
begin
Page:=TDownloadPage.create;
with Page do begin
Download('http://freepascal.ru/forum/posting.php?mode=reply&t=2527');
//Download('http://freepascal.ru/forum/login.php?redirect=posting.php&mode=reply&t=2527&sid=a4623f941081cee5a8e5a19748b435f0');
//Download('http://forum.pascal.net.ru/index.php');
headers.add('redirect: '+redirect+#13#10);
writeln(headers.text);
DocumentStrings.SaveToFile('d:\document.html');//<---измените пути
Headers.SaveToFile('d:\headers.html');//<--- и тут не забудьте..
destroy;
end;
readln;
end.
Вот запакованные модули synapse необходимые для компиляции:
(blcksock.pas, synautil.pas, synaip.pas, synsock.pas, synafpc.pas, synacode.pas)
http://doomed-game.narod.ru/Untis.rar