Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-10-25
5
Medium Priority
?
298 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 336 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 332 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 332 total points
ID: 9627642
Maybe do the download from a separate thread?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

926 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