开发者

programatically get the file size from a remote file using delphi, before download it

How i can determine the size in b开发者_开发知识库ytes of a remote file hosted in the web, before download it, using Delphi?

Thanks In advance.


You could use Indy.

First include IdHTTP.

You can retrieve the size this way:

procedure TFormMain.Button1Click(Sender: TObject);
var
  Http: TIdHTTP;
begin
  Http := TIdHTTP.Create(nil);
  try
    Http.Head('http://live.sysinternals.com/ADExplorer.exe');

    ShowMessage(IntToStr(Http.Response.ContentLength));
  finally
    Http.Free;
  end;
end;


Short answer: use the HTTP HEAD command, available in the TIdHttp component of Indy Delphi.

Details:

HTTP protocol defines a HEAD method.

9.4 HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

HEAD Asks for the response identical to the one that would correspond to a GET request, but without the response body, retrieving the complete response headers, without the entire content.

The HTTP response headers retrieved are documented in List of HTTP headers on Wikipedia. http://en.wikipedia.org/wiki/List_of_HTTP_headers

HTTP Headers form the core of an HTTP request, and are very important in an HTTP response. They define various characteristics of the data that is requested or the data that has been provided. The headers are separated from the request or response body by a blank line. HTTP headers can be near-arbitrary strings, but only some are commonly understood.

One of the headers that is always present for a valid URL to retrieve a content is the Content-Length header.

14.13 Content-Length

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

Content-Length = "Content-Length" ":" 1*DIGIT

An example is

Content-Length: 3495

Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

Any Content-Length greater than or equal to zero is a valid value. Section 4.4 describes how to determine the length of a message-body if a Content-Length is not given.

Note that the meaning of this field is significantly different from the corresponding definition in MIME, where it is an optional field used within the "message/external-body" content-type. In HTTP, it SHOULD be sent whenever the message's length can be determined prior to being transferred, unless this is prohibited by the rules in section 4.4.

From Delphi, drop a TIdHttp component to your form. And paste the following code in one of your delphi event process methods.

var 
  url: string;  // must contain a fully qualified url
  contentLength: integer;
begin
  ....
  contentLength:=0;
  try
    Idhttp1.Head(url);
    contentLength:=idhttp1.response.ContentLength;
  except end;
  ....


Be aware that not ALL servers will return a valid content size for a head request. If the content length = 0, then you will ONLY know if you issue a GET request. For example the HEAD request against the Google logo returns a 0 content-length, however a GET returns the proper length, but also retrieves the image. Some servers will return content-length as the length of the packet following the header.

You can use Synapse to get at this information also. Note that the data is transfered, but the buffer is thrown away. This is a much more reliable method, but at the cost of additional bandwidth.

var
  HTTP : tHTTPSend;
begin
  HTTP := THTTPSend.Create;
  try
    HTTP.HTTPMethod('GET',url);
    DownloadSize := HTTP.DownloadSize;
  finally
    HTTP.Free;
  end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜