Solved

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

Posted on 2003-10-25
5
286 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
Comment Utility
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
Comment Utility
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
Comment Utility
Maybe do the download from a separate thread?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

14 Experts available now in Live!

Get 1:1 Help Now