Q:

How do I get the size of a download when a HEAD request doesn't report ContentLength? 

Using this code in Delphi for getting a web page size: (I mean page source size)
uses
  IdHTTP

function URLsize(const URL : string) : integer;
var
  Http: TIdHTTP;
begin
  Http := TIdHTTP.Create(nil);
  try
    Http. Head(URL);    
    result := round(Http. Response. ContentLength / 1048576);   //MB   
  finally
    Http. Free;
  end;
end;

I can get file size easily for some URLs like http://sample.com/test.exe.  It returns the size in MB. 
But I cannot get URL size using this code for a URL like http://stackoverflow.com/; it returns 0 or -1. 
How can I get the size in that case? 

A:

Not all HTTP HEAD responses contain content-length.  So, what you are trying to do is impossible in general.  If you encounter a response that does not contain the content length you need to download the contents in order to find the length. 

A:

Even if a web server does return the proper content length, you're dividing it by 1048576 to get the megabyte value.  Since http://stackoverflow.com/ is much less than a single megabyte, it is returning 0.  I'm still stumped however where your -1 came from - because http://stackoverflow.com/ returns 194569 for me, without dividing.  Did you get a -1 from another website?  And are your results the divided value or the raw value from Http. Response. ContentLength?