Recent

Author Topic: [SOLVED] httpsend, ssl_openssl Download a file from https://wow.curseforge.com  (Read 3588 times)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 81
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/latest

Right now I am working with rvk code  from http://forum.lazarus.freepascal.org/index.php?topic=30344.0 .

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DownloadAddons(WebPage:string);
  2. var
  3.       HTTP: THTTPSend;
  4.       Redirected: boolean;
  5.       Filename: String;
  6.       Line: string;
  7.       i, j1, j2: integer;
  8.       DownloadAddon:string;
  9.     begin
  10.       Filename := 'unknown';
  11.       DownloadAddon:=C:\Users\dbabin\Downloads\WOW\new\;
  12.       HTTP := THTTPSend.Create;
  13.       try
  14.         repeat
  15.           Redirected := False;
  16.           HTTP.HTTPMethod('GET', WebPage);
  17.           case HTTP.Resultcode of
  18.             301, 302, 307:
  19.             begin
  20.               Memo1.Lines.LoadFromStream(HTTP.Document);
  21.               for i := 0 to HTTP.Headers.Count - 1 do
  22.                 if (Pos('location: ', lowercase(HTTP.Headers.Strings[i])) = 1) then
  23.                 begin
  24.                   WebPage := StringReplace(HTTP.Headers.Strings[i], 'location: ', '', []);
  25.                   HTTP.Clear;
  26.                   Redirected := True;
  27.                   break;
  28.                 end;
  29.             end;
  30.           end;
  31.         until not Redirected;
  32.  
  33.         // extract filename from headers
  34.         // content-disposition: attachment; filename="zzzzzzz.zzz"; filename*=UTF-8''zzzzzz.zzz
  35.         for i := 0 to HTTP.Headers.Count - 1 do
  36.           if (Pos('content-disposition: attachment;',
  37.             lowercase(HTTP.Headers.Strings[i])) = 1) then
  38.           begin
  39.             Line := HTTP.Headers.Strings[i];
  40.             j1 := pos('filename="', Line);
  41.             if j1 > 0 then
  42.             begin
  43.               j1 := j1 + length('filename="');
  44.               j2 := j1;
  45.               while (j2 < length(Line)) and (Line[j2] <> '"') do
  46.                 Inc(j2);
  47.               if j2 <= length(Line) then
  48.                 Filename := Copy(Line, j1, j2 - j1);
  49.             end;
  50.           end;
  51.  
  52.         HTTP.Document.SaveToFile(DownloadAddon + Filename);
  53.  
  54.       finally
  55.         HTTP.Free;
  56.       end;
  57.       ShowMessage('Done');
  58. end;    
  59.  

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




   

« Last Edit: September 27, 2018, 07:25:22 am by TheLastCayen »

balazsszekely

  • Guest
Re: httpsend, ssl_openssl Download a file from https://wow.curseforge.com
« Reply #1 on: September 27, 2018, 06:32:17 am »
Quote
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?
Please test attached project. You should change the values in "Button1click" according to your needs.

PS: The code can be further optimized. If you are on windows and you don't have the openssl dll's, the application will download it for you, however there is no visual feedback for this. Also you should create a new unit with a TDownload class, where all the download related stuff is implemented. Now everything is in the main unit...looks ugly.
« Last Edit: September 27, 2018, 06:40:19 am by GetMem »

TheLastCayen

  • Jr. Member
  • **
  • Posts: 81
Re: httpsend, ssl_openssl Download a file from https://wow.curseforge.com
« Reply #2 on: September 27, 2018, 07:23:48 am »
Oh wow, this is better than I was expecting.
Thank you Very much GetMem, I am really impressed with your quick answer and the work you did. I tested it with few other site and it works perfectly:)

Have a great day.

 

TinyPortal © 2005-2018