Hi Everyone,
I am using
Lazarus 1.8.4
FPC Version 3.0.4
Windows 7 64
openssl-1.0.2j-x64_86-win64.zip
synalist-code-r209-trunk
I am trying to download a file from
https://wow.curseforge.com. The site do a redirection instead of a direct download. For my example I will use
https://wow.curseforge.com/projects/vuhdo/files/latestRight now I am working with rvk code from
http://forum.lazarus.freepascal.org/index.php?topic=30344.0 .
procedure TForm1.DownloadAddons(WebPage:string);
var
HTTP: THTTPSend;
Redirected: boolean;
Filename: String;
Line: string;
i, j1, j2: integer;
DownloadAddon:string;
begin
Filename := 'unknown';
DownloadAddon:=C:\Users\dbabin\Downloads\WOW\new\;
HTTP := THTTPSend.Create;
try
repeat
Redirected := False;
HTTP.HTTPMethod('GET', WebPage);
case HTTP.Resultcode of
301, 302, 307:
begin
Memo1.Lines.LoadFromStream(HTTP.Document);
for i := 0 to HTTP.Headers.Count - 1 do
if (Pos('location: ', lowercase(HTTP.Headers.Strings[i])) = 1) then
begin
WebPage := StringReplace(HTTP.Headers.Strings[i], 'location: ', '', []);
HTTP.Clear;
Redirected := True;
break;
end;
end;
end;
until not Redirected;
// extract filename from headers
// content-disposition: attachment; filename="zzzzzzz.zzz"; filename*=UTF-8''zzzzzz.zzz
for i := 0 to HTTP.Headers.Count - 1 do
if (Pos('content-disposition: attachment;',
lowercase(HTTP.Headers.Strings[i])) = 1) then
begin
Line := HTTP.Headers.Strings[i];
j1 := pos('filename="', Line);
if j1 > 0 then
begin
j1 := j1 + length('filename="');
j2 := j1;
while (j2 < length(Line)) and (Line[j2] <> '"') do
Inc(j2);
if j2 <= length(Line) then
Filename := Copy(Line, j1, j2 - j1);
end;
end;
HTTP.Document.SaveToFile(DownloadAddon + Filename);
finally
HTTP.Free;
end;
ShowMessage('Done');
end;
If I understand this code look for the header to get the file name and download it. But instead my code create the empty unknown file. I am assuming curseforge use a different way to send the file then Dropbox. This is why I create a Memo1 and try to load the content of HTTP.Document so I can modify the code. But memo1 stay empty.
Anyone know what I am doing wrong when I am trying to read the HTTP.Document? Or there is a better way to download the file?
Thank you