[SOLVED] Get file zize of GitHub release download lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- lainz · 2016-08-17 14:09 Hi, I've used the code from here: http://forum.lazarus.freepascal.org/index.php/topic,21824.msg128329.ht ml#msg128329 To make this: 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 uses  fphttpclient; function GetSizeOfDownload: integer;var  client: TFPHTTPClient;  s, url, username, password: string;  i: integer;  VSize: int64 = -1;begin  client := TFPHTTPClient.Create(nil);  url := "https://github.com/lainz/lainzcodestudio/releases/download/v0.1/lainz codestudio-0.1-win32.zip";  username := "";  password := "";  if username <> '' then    client.UserName := username;  if password <> '' then    client.Password := password;  client.AllowRedirect := True;  try    client.HTTPMethod('HEAD', url, nil, [200]);    for i := 0 to pred(client.ResponseHeaders.Count) do    begin      s := UpperCase(client.ResponseHeaders[i]);      if Pos('CONTENT-LENGTH:', s) > 0 then      begin        VSize := StrToIntDef(Copy(s, Pos(':', s) + 1, Length(s)), -1);        Break;      end;    end;  finally    client.Free;  end;  Result := VSize;end;   The code works for http and https (I have the dll's in the project folder downloaded from here: https://slproweb.com/products/Win32OpenSSL.html). I'm developing a 32 bit application and using the 32 bit dlls for Windows. I can test file size of sourceforge https and images from https, but seems that github is special, it returns 403 forbidden. Maybe because (I don't really know) I'm trying to access that data from code and not from a browser. BTW from a third party application called AutoPlayMedia Studio (AMS) with a function that comes with it called HTTP.GetFileSizeSecure I can access that data on github (https://www.indigorose.com/webhelp/tu/Prog ram_Reference/Actions/HTTP.GetFileSizeSecure.htm). But I need to do it from Lazarus, and if possible only using fphttpclient. Any ideas? ---------------------------------------------------------------------- engkin · 2016-08-17 14:29 You are not providing a User-Agent, which I believe is required. ---------------------------------------------------------------------- lainz · 2016-08-17 14:30 Quote from: engkin on August 17, 2016, 04:29:19 pmYou are not providing a User-Agent, which I believe is required. You know how to do that? ---------------------------------------------------------------------- engkin · 2016-08-17 14:34 You, most likely, will like this post. ---------------------------------------------------------------------- lainz · 2016-08-17 14:50 Quote from: engkin on August 17, 2016, 04:34:15 pmYou, most likely, will like this post. Thanks, I will try. ---------------------------------------------------------------------- rvk · 2016-08-17 14:55 I don't think the User-Agent is the problem. You can use client.get(url) to get the file without the User-Agent. I think the problem is that HEAD is not allowed on the AmazonS3 server. ---------------------------------------------------------------------- derek.john.evans · 2016-08-17 14:56 It looks like GitHub does not accept a redirected HEAD request. You could try a standard get, and abort when data starts downloading. 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.DoDataReceived(Sender: TObject; const ContentLength, CurrentPos: int64);begin  if ContentLength > 0 then begin    Abort;  end;end; procedure TForm1.Button1Click(Sender: TObject);var  LStringStream: TStringStream;begin  LStringStream := TStringStream.Create('');  try    with TFPHTTPClient.Create(nil) do begin      try        OnDataReceived := @DoDataReceived;        AllowRedirect := True;        ResponseHeaders.NameValueSeparator := ':';        try          HTTPMethod('GET',            'https://github. com/lainz/lainzcodestudio/releases/download/v0.1/lainzcodestudio-0.1- win32.zip',            LStringStream, []);        except        end;      ShowMessage(ResponseHeaders.Values['CONTENT-LENGTH']);      finally        Free;      end;    end;  finally    FreeAndNil(LStringStream);  end;end; ---------------------------------------------------------------------- lainz · 2016-08-17 15:05 Yes, using the User-Agent doesn't works. I will try the Geepster code. Edit: It works, thanks! ---------------------------------------------------------------------- Thaddy · 2016-08-17 18:52 Quote from: Geepster on August 17, 2016, 04:56:07 pmIt looks like GitHub does not accept a redirected HEAD request. Yes. That's the case. Nobody should. It is best practice.