Recent

Author Topic: Read text file from HTTP URL  (Read 16194 times)

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Read text file from HTTP URL
« on: March 20, 2012, 08:47:25 am »
Hi Coders!

I can read simple txt file with this code:
Code: [Select]
program ReadFile;
uses
 Sysutils;
Var UserFile : Text;
    FileName, TFile : String;
Begin
  AssignFile(UserFile, 'File.txt');
  //AssignFile(UserFile, 'http://192.168.0.3/file.txt');
  Reset(UserFile);
  Repeat
    Readln(UserFile,TFile);
    Writeln(TFile);
  Until Eof(UserFile);
  Close(UserFile);
  Readln;
End.

But, how can I read file from http URL? If I try it, the program stop working.

Thank you!

Relative

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Read text file from HTTP URL
« Reply #1 on: March 20, 2012, 09:13:05 am »
You are going to need to download the file first. At least for Delphi someone made a simple component HTTPGet, but i don't think it compiles with freepascal
http://www.utilmind.com/delphi2.html

Anyway, you need a unit/class that can download stuff.

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Read text file from HTTP URL
« Reply #2 on: March 20, 2012, 09:16:41 am »

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #3 on: March 20, 2012, 09:29:37 am »
Thank you. I'm checking...

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Read text file from HTTP URL
« Reply #4 on: March 20, 2012, 09:39:31 am »
Try this simple routine:
Code: [Select]

function DownloadURL_NOCache(const aUrl: string;
                                   var s: String;
                                   Progress: cbProgressBar): Boolean;
Const
  sBUF  = 32000;  // 1024
var
   hSession: HINTERNET;
   hService: HINTERNET;
   lpBuffer: array[0..sBUF + 1] of Char;
   dwBytesRead: DWORD;
   i:           LongInt;
begin
   Result := False;
   s := '';
   i := 0;
   if Progress <> nil then
     Progress(aURL, i);
   // hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
   hSession := InternetOpen('MyApp',
                            INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
   try
     if Assigned(hSession) then
     begin
       hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0,
                                   INTERNET_FLAG_RELOAD, 0);
       if Assigned(hService) then
         try
           while True do
           begin
             dwBytesRead := sBuf;
             InternetReadFile(hService, @lpBuffer, sBUF, dwBytesRead);
             if dwBytesRead = 0 then break;
             lpBuffer[dwBytesRead] := #0;
             s := s + lpBuffer;
             I := i + 1;
             if i > 99 then i := 1;
             if Progress <> nil then
               Progress(aURL, i);
           end;
           Result := True;
         finally
           InternetCloseHandle(hService);
         end;
     end;
   finally
     InternetCloseHandle(hSession);
     if Progress <> nil then
       Progress(aURL, 99);
   end;
end;
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #5 on: March 20, 2012, 09:47:39 am »
Try this simple routine:

I get lot of error messages:
Code: [Select]
fxc_cloner_v036.pas(10,59) Error: Identifier not found "cbProgressBar"
fxc_cloner_v036.pas(14,23) Error: Identifier not found "HINTERNET"
fxc_cloner_v036.pas(14,23) Error: Error in type definition
fxc_cloner_v036.pas(15,23) Error: Identifier not found "HINTERNET"
fxc_cloner_v036.pas(15,23) Error: Error in type definition
fxc_cloner_v036.pas(23,16) Error: Operator is not overloaded
fxc_cloner_v036.pas(24,14) Error: Illegal expression
fxc_cloner_v036.pas(24,14) Fatal: Syntax error, ";" expected but "(" found

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Read text file from HTTP URL
« Reply #6 on: March 20, 2012, 10:04:37 am »
sorry, my bad

You will need wininet in the uses clause.

The progressbar references are a call back to another window that has a status bar ...  set to nil for now
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #7 on: March 20, 2012, 10:10:30 am »
Wininet done. It works.

But how do you mean: 'set to nil for now'?

Sorry, I'm a very beginner Pascal user.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Read text file from HTTP URL
« Reply #8 on: March 20, 2012, 10:19:08 am »
Note: I don't think wininet will be cross-platform. If you want to have cross-platform support, look at e.g. synapse or lnet.

Set Bla to nil would be something like:
Code: [Select]
Bla:=nil;
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Read text file from HTTP URL
« Reply #9 on: March 20, 2012, 10:29:21 am »
Wininet done. It works.

But how do you mean: 'set to nil for now'?

Sorry, I'm a very beginner Pascal user.

Off to bed now... I will show you the progressBar code tomorrow.  Its not proportional to the the task ... just lets users know its working and not stalled.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #10 on: March 20, 2012, 10:32:04 am »
Ok. Thank you in advance. Good night!

joseme

  • Full Member
  • ***
  • Posts: 128
    • Logosoft sistemas
Re: Read text file from HTTP URL
« Reply #11 on: March 20, 2012, 01:31:41 pm »
You can use Indy for this task, it may be simpler:

Code: [Select]
function TfrmActualiza.descargarActualizacion(OrigFile, DestFile: String): Boolean;
var f:tfilestream;
  IdHTTP1: TIdHTTP;
begin
  IdHTTP1 := TIdHTTP.Create(nil);
  f:=tfilestream.create(destFile, fmcreate);
  try
    idhttp1.head(origFile);   //To know if file exists
    idhttp1.get(origFile, f); // and retrieve it
  except
    ShowMessage('File '+origFile+' does not exist or is unretrievable');
    Result := False;
  end;
    freeandnil(f);
    IdHTTP1.Destroy;
end;
un aporte a la comunidad:
http://pascalylazarus.blogspot.com/

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #12 on: March 20, 2012, 02:46:08 pm »
You can use Indy for this task, it may be simpler:

Could you help me please! How can I use this code? I got some error messages:
Code: [Select]
fxc_cloner_v037.pas(8,24) Error: Identifier not found "TfrmActualiza"
fxc_cloner_v037.pas(8,46) Error: class identifier expected
fxc_cloner_v037.pas(9,18) Error: Identifier not found "tfilestream"
fxc_cloner_v037.pas(9,18) Error: Error in type definition
fxc_cloner_v037.pas(10,19) Error: Identifier not found "TIdHTTP"
fxc_cloner_v037.pas(10,19) Error: Error in type definition
fxc_cloner_v037.pas(12,21) Error: Identifier not found "TIdHTTP"
fxc_cloner_v037.pas(13,17) Error: Identifier not found "tfilestream"
fxc_cloner_v037.pas(13,43) Error: Identifier not found "fmcreate"
fxc_cloner_v037.pas(15,13) Error: Illegal qualifier
fxc_cloner_v037.pas(16,13) Error: Illegal qualifier
fxc_cloner_v037.pas(18,16) Error: Identifier not found "ShowMessage"
fxc_cloner_v037.pas(22,13) Error: Illegal qualifier

joseme

  • Full Member
  • ***
  • Posts: 128
    • Logosoft sistemas
Re: Read text file from HTTP URL
« Reply #13 on: March 20, 2012, 02:59:25 pm »
That is because TfrmActualiza is the name of the form in my project, you should replace with your own form name. Also, you should add IdHTTP and FileUtils to the uses of your unit.
un aporte a la comunidad:
http://pascalylazarus.blogspot.com/

Relative

  • Jr. Member
  • **
  • Posts: 56
  • Sedo est anima rerum.
    • FxCoder
Re: Read text file from HTTP URL
« Reply #14 on: March 20, 2012, 03:18:33 pm »
I added idHTTP to the uses and I got:
Code: [Select]
fxc_cloner_v037.pas(6,22) Fatal: Can't find unit IdHTTP used by fxc_cloner_v037

 

TinyPortal © 2005-2018