Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-25
5
Medium Priority
?
293 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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