Solved

Wininet downloading file .When I start with downloading my project is blocked (freezed).

Posted on 2003-10-25
5
287 Views
Last Modified: 2010-04-03
I use Wininet lib. for downloading file from internet. When I start with downloading my project is blocked (freezed).
How can i fix this problem?

I use this method for downloading file:

{
var
hInternetO,hHInternetC,hResource:HINTERNET;
FileStream:TFileStream;
DataProceed:array[0..1023] of Char;
NumRead,i:Cardinal;
kraj:Boolean;
begin

  hInternetO:=InternetOpen( 'Naslov',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0 );
  hHInternetC:=InternetConnect( HInternetO,PAnsiChar(Host),INTERNET_DEFAULT_HTTP_PORT,nil,nil,INTERNET_SERVICE_HTTP,0,0);
  hResource := HttpOpenRequest(hHInternetC, 'GET', PChar(Resource), nil, nil, nil, 0, 0);
  HttpSendRequest(hResource, nil, 0, nil, 0);

  ///
   try
     FileStream := TFileStream.Create('c:\', FileOpenModes[FileExists('c:\')]);
    except
     ShowMessage('Please enter valid URL !');
     Exit;
    end;
  try
    FileStream.Position := Result;
    repeat
      ZeroMemory(@DataProceed, SizeOf(DataProceed));

      InternetReadFile(hResource, @DataProceed, SizeOf(DataProceed)-1, NumRead);

      if (NumRead <= 0) then Break;
      FileStream.Write(DataProceed, NumRead);
      Result := Result + Integer(NumRead);

    until Kraj=true;
  finally
    FileStream.Free();
    InternetCloseHandle(hHInternetC);
    InternetCloseHandle(hInternetO);
  end;
}

Thank you.
0
Comment
Question by:nikola_mk
5 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 84 total points
ID: 9622495
It sounds as if the connection is "blocking". Try asynchronous operations - put INTERNET_FLAG_ASYNC in the flags, ie   hInternetO:=InternetOpen( 'Naslov',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,INTERNET_FLAG_ASYNC );

Geoff M.
0
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 83 total points
ID: 9623806
or put it in a thread - what I'd do
instead of all this code you can use URLDownloadToFile or Indy HTTP with Get
here's the code of my download unit


unit DownloadFileU;

{.$DEFINE UseIndy}

interface


function DownloadFile(const AURL, AFileName: string): Integer;

implementation

uses Classes, {$IFDEF UseIndy} IdHTTP, Windows; {$ELSE} Sysutils, UrlMon; {$ENDIF}

function DownloadFile(const AURL, AFileName: string): Integer;
{$IFDEF UseIndy}
var ms: TMemoryStream;
{$ELSE}

  function GetFileSize(const AFile: string): Integer;
  begin
       with TFileStream.Create(AFile, fmOpenRead) do
       try Result:=Size;
       finally Free; end;
  end;

{$ENDIF}
begin
     {$IFDEF UseIndy}
     with TIdHTTP.Create(nil) do
     try
        ms:=TMemoryStream.Create;
        try
           try
              Get(AURL, ms);
              ms.SaveToFile(AFileName);
              Result:=ms.Size;
           except
              Result:=-2;
           end;
        finally
           ms.Free;
        end;
     finally
        Free;
     end;
     {$ELSE}
     try
        if URLDownloadToFile(nil, PChar(AURL), PChar(AFileName), 0, nil) = 0 then
           Result:=GetFileSize(AFileName)
        else
           Result:=-1;
     except
        Result:=-2;
     end;
     {$ENDIF}
end;

end.
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 83 total points
ID: 9627642
Maybe do the download from a separate thread?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now