Wininet.dll & HTTPQueryInfo

Giganews Newsgroups
Subject: Wininet.dll & HTTPQueryInfo
Posted by:  MikeR (nf4lNoSp…@pobox.com)
Date: Fri, 09 Mar 2007

Using Wininet.dll, I can download a file using the following URL and code.
I'd like to get the filesize using HTTPQueryInfo so I can implement a progress
bar. HTTPQueryInfo returns ERROR_HTTP_HEADER_NOT_FOUND.

I believe the file is created on the fly. The URL up to the filename would be
the same, and the filename could be the same for any user. I assume (because the
server owner won't communicate) that the directory is changed when the file is
requested. Any hope of getting this to work?
Mike

'https://p1k.domain.org/xyzuser/xyzreport.adi?login=myID&password=mypw&par_query=1&par_withown=yes&par_detail=yes/xyzreport.adi'

NetHandle := InternetOpen('Delphi 7.x',INTERNET_OPEN_TYPE_PRECONFIG, nil,nil,0);
  if Assigned(NetHandle) then
  begin
    UrlHandle :=
InternetOpenUrl(NetHandle,PChar(Url),nil,0,INTERNET_FLAG_RELOAD, 0);
    if Assigned(UrlHandle) then
    begin
      dummy := 0;
      bufLen := Length(infoBuffer);
    if
HttpQueryInfo(UrlHandle,HTTP_QUERY_CONTENT_LENGTH,@infoBuffer[0],bufLen,dummy) then
    reply := infobuffer
    else
    CheckInetResult(false);
    FileHandle := CreateFile(PChar(FileName), GENERIC_WRITE, FILE_SHARE_WRITE,
          nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (FileHandle = INVALID_HANDLE_VALUE) then
      RaiseLastOSError;
    Buffer := StrAlloc(ChunkSize);
    repeat
      FillChar(Buffer^, ChunkSize, 0);
      InternetReadFile(URLhandle, Buffer, ChunkSize, BytesRead);
      Win32Check(WriteFile(FileHandle, Buffer^, BytesRead, BytesWritten, nil));
      if BytesRead > 0 then
        result := true;
    until BytesRead = 0;
    InternetCloseHandle(UrlHandle);
    StrDispose(Buffer);
    CloseHandle(FileHandle);
  end;

Replies