title: Delphi - File size by URL - error 12150
body: <p>Delphi 6 Prof, Win7 OS.</p>
  
  <p>I have this code to get the file size by URL:</p>
  
  <pre><code>function TDDWIToolObject.GetFileSize(out Size: Int64): boolean;
  var
      hInet: HINTERNET;
      hRequest : HINTERNET;
      lpdwBufferLength: DWORD;
      lpdwReserved    : DWORD;
      ServerName: string;
      Resource: string;
      FileSizeBuffer : array[0..32] of char;
      //SizeCard : Cardinal;
  begin
      ParseURL(Url, ServerName, Resource);
      Result := False;
      Size := 0;
  
      hInet := InternetOpen(PChar(_UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      if hInet=nil then begin
          FErrorCode := GetLastError;
          Exit;
      end;
  
      try
  
  
          hRequest := InternetOpenUrl(hInet, PChar(URL), PCHar(Headers), Length(HEaders), 0, 0);
  
          try
  
              FillChar(FileSizeBuffer, SizeOf(FileSizeBuffer), #0);
              lpdwBufferLength := SizeOf(FileSizeBuffer);
              lpdwReserved :=0;
  
              if not HttpQueryInfo(
                  hRequest,
                  HTTP_QUERY_CONTENT_LENGTH,
                  @FileSizeBuffer, lpdwBufferLength, lpdwReserved) then begin
  
                  FErrorCode:=GetLastError;
                  Exit;
              end;
  
              Size := StrToInt64(StrPas(FileSizeBuffer));
              Result := True;
  
          finally
              InternetCloseHandle(hRequest);
          end;
  
      finally
          InternetCloseHandle(hInet);
      end;
  
  end;
  </code></pre>
  
  <p>It is working good with my machine + DSL connection.</p>
  
  <p>But when I check this code in other place which is fully protected (proxy + many policies) then I get error code 12150... :-(</p>
  
  <p>What is interesting, that I can download this file, when I use this code:</p>
  
  <pre><code>function TDDWIToolObject.DownloadFile;
  var
      hInet: HINTERNET;
      hFile: HINTERNET;
      pbuffer: Pointer;
      bytesRead: DWORD;
      Stm : TFileStream;
      TotalBytes : Int64;
      AbortIt : boolean;
  begin
      Result := False;
      FErrorCode := -1;
      FAborted := False;
      hInet := InternetOpen(PChar(_UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      if hInet = nil
          then begin
              FErrorCode := GetLastError;
              Exit;
          end;
      try
          hFile := InternetOpenURL(hInet, PChar(URL), PChar(FHeaders), Length(FHeaders), 0, 0);
          if hFile = nil
              then begin
                  FErrorCode := GetLastError;
                  Exit;
              end;
          try
              Stm := TFileStream.Create(FN, fmCreate);
              try
                  GetMem(pbuffer, FBufferSize);
                  try
                      TotalBytes := 0; AbortIt := False;
                      while (not FAborted) do begin
                          if not InternetReadFile(hFile, pbuffer, FBufferSize, bytesRead) then begin
                              FErrorCode := GetLastError;
                              Exit;
                          end;
                          if bytesRead &gt; 0 then begin
                              Stm.WriteBuffer(pbuffer^, bytesRead);
                              if Assigned(FOnBytesArrived)
                                  then begin
                                      inc(TotalBytes, bytesRead);
                                      FOnBytesArrived(Self, TotalBytes, AbortIt);
                                      if AbortIt
                                          then Abort;
                                  end;
                          end else begin
                              break;
                          end;
                      end;
                  finally
                      FreeMem(pbuffer);
                  end;
                  if not FAborted
                      then Result := True;
              finally
                  Stm.Free;
              end;
          finally
              InternetCloseHandle(hFile);
          end;
      finally
          InternetCloseHandle(hInet);
      end;
  end;
  </code></pre>
  
  <p>It is interesting because sysadmin allowed the requests to this URL in proxy, and the downloads work for me - only the content length requests fail.</p>
  
  <p>I want to ask one question, but it is hard to do, because many of them is in my mind...
  Do you have some idea what we can do to allow the requests?
  Maybe HttpRequest uses another port, or some "illegal" what isn't proxiable? 
  Or my code is seems to be wrong? Why it is working in nonprotected area? What are the differents between download and get content length requests?</p>
  
  <p>So the question is as in summarized format: do you have some idea what can I do to I get the file size before downloading?</p>
  
  <p>Thanks for it.</p>