Solved

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

Posted on 2003-10-25
5
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month5 days, 14 hours left to enroll

626 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