Recent

Author Topic: Reading a text file from a web-site.  (Read 29828 times)

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #45 on: December 12, 2017, 10:48:59 pm »
Are you sure that other files (with spaces in their name) succeed with downloading ?
Just a quick response again Molly - I'm having a very busy time with a totally unrelated (urgent) engineering project so not able to properly evaluate your suggestions but I am sure that I've downloaded files that have spaces in their names.

I also understand that replacing spaces with %20 may well prove necessary but that may well involve some post-download processing to return them as well (just guessing)  -  not that there is any problem as far as time taken really.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading a text file from a web-site.
« Reply #46 on: December 12, 2017, 10:56:17 pm »
Don't worry J-G and please take your time. Have fun with the engineering   8)

Feel free 2 ping in case of issues or wanting to get back to things.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #47 on: January 03, 2018, 05:50:17 pm »
Having sorted my 'End of Year' accounts and awaiting decisions about the engineering project I'm looking at this problem again.  :)

I understand the issue of the 'scope' but have now come to a further impasse which an hour+ effort in re-positioning  [end]  hasn't resolved.

Here is the latest 'Download2File' procedure...

Code: Pascal  [Select][+][-]
  1. function  Download2File(URL: String; Filename: String): boolean;
  2. Var
  3.   SC : byte;
  4. begin
  5.   Result := false;
  6.   with TFPHttpClient.Create(Nil) do
  7.     try
  8.       AllowRedirect := true;
  9.       Get(U,F);
  10. //      Get(URL, FileName);
  11.       Result := true;
  12.     except
  13.  
  14.        // Exception handeling
  15.  
  16.       if ResponseStatusCode = 200 then
  17.         begin
  18.           //success, do other things if needed
  19.           Result := True;
  20.         end
  21.       else
  22.         begin
  23.           SC := ResponseStatusCode Div 100;
  24.           case SC of
  25.             1 : begin
  26.                 end;
  27.             2 : begin
  28.                 end;
  29.             3 : begin
  30.                 end;
  31.             4 : begin
  32.                   showmessage('Copy of '+F+' Failed with Code'+IntToStr(ResponseStatusCode));
  33.                 end;
  34.             5 : begin
  35.                 end;
  36.           end;
  37.         end;
  38.     end;
  39.  
  40.   finally
  41.  
  42.     Free;
  43.   end;
  44. end;
  45.  

... which throws two compile-time errors.

At line 40    (Finally)    Illegal expression   
and line 42  (Free;)     Syntax error, ";" expected but "identifier FREE" found

I've tried removing 'end' at line 38 but a CR at line 37 causes it to be automatically re-created which tells me that it is needed.

I can't see anything that I've missed at the start of the procedure which seems to be the next most logical issue.

To get around the issue of spaces in file names I'm considering replacing them with underscores  -  I have complete control over the names used so it's no big deal.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Reading a text file from a web-site.
« Reply #48 on: January 03, 2018, 06:05:44 pm »
Misplaced except in line 12?
Within except there is handling of  ResponseStatusCode = 200, which means successful receiving.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading a text file from a web-site.
« Reply #49 on: January 03, 2018, 06:23:37 pm »
Misplaced except in line 12?
Within except there is handling of  ResponseStatusCode = 200, which means successful receiving.
Yes and no. The except block is there on purpose.

What i did not explicitly mentioned to J-G (expected to do some homework  ;) ) is that the except block should handle the exceptions that J-G wants to catch there.

That is why in one of my examples _shows_ which exact exception occurs.

An except block act similar like a case statement:
Code: [Select]
except
  on E: Exception do  // This will catch _all_ exceptions also those specific to THTTPClient
  begin
 
 end
  on E: Otherexception do
  begin
  end
  else // what to do when none of the above cases matched the specific exception and we're stuck here ? e.g. all so far unhandled exceptions
  begin
  end;
So, in case you wish to 'catch' a httpclient specific exception you would have to add that to the exception case list

And fwiw you need to check ResponseStatusCode if you wish to make sure that the request was handled ok as there are other errors that do not raise an exception (or can be 'removed' from the exception list).

So, depending on how you handle the exception J-G could move that code after the exception handler.

edit:
some reading material on exceptions here and here.

fwiw: You should 'catch' EHTTPClient exception, and decide what you wish to do with it, e.g. silently ignore, handle, or (re)raise.
« Last Edit: January 03, 2018, 06:54:30 pm by molly »

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #50 on: January 03, 2018, 06:52:41 pm »
I'm happy to do homework Molly   ::)   but until I can get past this 'Illegal Expression' at line 40 I have no idea what to research.

I had tried inserting
Code: Pascal  [Select][+][-]
  1.       on E: Exception do
  2.         begin
  3.           // Exception handeling
  4.           showmessage('Exception - '+ E.Message);
  5.         end;
  6.  
... at line 13 but that returned the 'outside scope' issue for ResponseStatusCode.

moving the [end] to line 39 sorted that but re-instated the 'Illegal Expression' at line 40.

Once I can get this to compile I can deal with the plethora of exceptions via the case statement at line 24  -  I just can't fathom why I get the 'Illegal Expression' at [Finally].

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading a text file from a web-site.
« Reply #51 on: January 03, 2018, 06:56:20 pm »
@J-G:
Could you past your the code with latest addition of showing a message on E: Exception ? I was unable to reproduce based on your instructions on what you did (my clumsiness)

PS: i added some links to (external) reading material. Seems that our wiki is a bit lacking on that subject (or i missed it).
« Last Edit: January 03, 2018, 06:57:51 pm by molly »

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #52 on: January 03, 2018, 07:07:25 pm »
Code: Pascal  [Select][+][-]
  1. function  Download2File(URL: String; Filename: String): boolean;
  2. Var
  3.   SC : byte;
  4. begin
  5.   Result := false;
  6.   with TFPHttpClient.Create(Nil) do
  7.     try
  8.       AllowRedirect := true;
  9.       Get(U,F);
  10. //      Get(URL, FileName);
  11.       Result := true;
  12.     except
  13.       on E: Exception do
  14.         begin
  15.           // Exception handeling
  16.           showmessage('Exception - '+ E.Message);
  17. //      end;
  18.           if ResponseStatusCode = 200 then
  19.             begin
  20.               //success, do other things if needed
  21.               Result := True;
  22.             end
  23.           else
  24.             begin
  25.               SC := ResponseStatusCode Div 100;
  26.               case SC of
  27.                 1 : begin
  28.                     end;
  29.                 2 : begin
  30.                     end;
  31.                 3 : begin
  32.                     end;
  33.                 4 : begin
  34.                       showmessage('Copy of '+F+' Failed with Code'+IntToStr(ResponseStatusCode));
  35.                     end;
  36.                 5 : begin
  37.                     end;
  38.               end;
  39.             end;
  40.         end;
  41. //    end;
  42.  
  43.   finally
  44.  
  45.     Free;
  46.   end;
  47. end;
  48.  

I've commented out the two [end] statements where I think at least one of them should be - at lines 17 and 41.

With 17 'live' the scope issue returns - with 41 'live' the 'Illegal Expression' returns
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading a text file from a web-site.
« Reply #53 on: January 03, 2018, 07:21:11 pm »
Ah, my bad J-G. It was already wrong in your first code you posted today.

You are using a nested try except finally block, so you require two try statements. Your current code only has one. That should solve your current issue.

The rest seems ok, but change the E : Exception into E: EHttpClient exception. Imagine that you do not receive a exception related to httpclient but (imaginary) I/O error. Then your code is attempting to cover the EHTTPClient exception, instead of the (imaginary) I/O exception.

Like this:
Code: [Select]
function  Download2File(URL: String; Filename: String): boolean;
Var
  SC : byte;
begin
  Result := false;
  with TFPHttpClient.Create(Nil) do
  try
    try
      AllowRedirect := true;  // can be located in the first try of the try-finally block
      Get(URL, FileName);
      Result := true;
    except         // Exception handling
      on E: EHTTPClient do  // handling httpclient specific exceptions
      begin
        if ResponseStatusCode = 200 then
        begin
          //success, do other things if needed
          Result := True;
        end
        else 
        begin
          SC := ResponseStatusCode Div 100;
          case SC of
            1 : begin
                end;
            2 : begin
                end;
            3 : begin
                end;
            4 : begin
//                  showmessage('Copy of '+F+' Failed with Code'+IntToStr(ResponseStatusCode));
                end;
            5 : begin
                end;
          end;
        end;
      end
      else  // All other exceptions, not being of type EHttpClient
      begin
        WriteLn('Oops an exception that we can''t handle because we do not know which one it is or do not want to handle it');
        Raise;  // re-raise the exception instead
      end;
    end
  finally
    Free;
  end;
end;

edit: some other small remarks added to code.
« Last Edit: January 03, 2018, 07:42:23 pm by molly »

guest61674

  • Guest
Re: Reading a text file from a web-site.
« Reply #54 on: January 03, 2018, 07:23:35 pm »
try
try
finally
end;
except
end;

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Reading a text file from a web-site.
« Reply #55 on: January 03, 2018, 07:25:34 pm »
There is a try missing for the finally, that gives the illegal expression error.

Read this thread how to use try-finally-end and try-except-end (even nested):

http://forum.lazarus.freepascal.org/index.php/topic,4987.msg23808.html


J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #56 on: January 03, 2018, 08:22:06 pm »
Thanks Molly   -  and Heriberto & Lutz

Sometimes one cannot see the wood for the trees  :-[

I've looked at the links you supplied (thanks -  googling requires one to ask the right question  :() and had in fact come to the 'nested' conclusion as a probability but I've just had a monitor PSU fail (less than a month old!  - and that replaced one that died after 18 days!)  so have been messing about with moving cables etc. just to see my code on the second monitor to test --  and yes it's compiled -- so I can move forward. :D

Your suggested additions are all useful and will form part of my final solution, thanks.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading a text file from a web-site.
« Reply #57 on: January 04, 2018, 01:35:43 am »
Some progress.

Most files - with spaces in the name - are downloaded correctly without throwing an error.

ie.  No problem with :-
JG Angels From the Realms.PDF     
Mary Poppins A.CAP
God Bless Us.MP3
Feliz.MP3

These throw a 400 exception :-
Hark The Herald.MP3
Lo How a Rose.CAP

To find out if the actual file is a problem I renamed  Feliz.MP3  to   Hark The Herald.MP3  and that threw the exception !! :o

Further tests showed that  Hark_The_Herald.MP3   and   Lo_How_a_Rose.CAP  download without an issue.

My simple mind tells me that there is no logic to that  %) 

The exceptions are being trapped and I now have to determine what action to take at that time. Currently I'm just showing a message which requires input from the user for the program to continue but if I could determine why Hark_The_Herald works but Hark The Herald doesn't I might be able to modify something and re-try.

The clunky   'Replace a space with an underscore'  is do-able but isn't a good answer.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Reading a text file from a web-site.
« Reply #58 on: January 04, 2018, 02:01:45 am »
hello,
have you tried to replace  the space characters with %20 in your string url request ?

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Reading a text file from a web-site.
« Reply #59 on: January 04, 2018, 02:28:01 am »
Have you checked, that there are no double spaces either in the name of the real file or in the url? Maybe there is a problem with capitalization if the http server to read from runs not on windows.

 

TinyPortal © 2005-2018