[SOLVED] httpsend, ssl_openssl Download a file from https://wow.curseforge.com lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- TheLastCayen · 2018-09-26 12:52 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 ---------------------------------------------------------------------- balazsszekely · 2018-09-27 04:32 QuoteAnyone 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. ---------------------------------------------------------------------- TheLastCayen · 2018-09-27 05:23 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.