Recent

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

GAN

  • Sr. Member
  • ****
  • Posts: 382
Re: Reading a text file from a web-site.
« Reply #60 on: January 04, 2018, 03:18:30 am »
May you use TRIM function to avoid spaces at the begin and end of the string.
Linux Mint Mate (allways)
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite - LazReport

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #61 on: January 04, 2018, 03:46:45 pm »
Thanks all for your input but I still can't fathom the logic  -- or rather, lack of  ::)
Quote from: Lutz Mändle
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.
Yes - and I know that the server runs on Linux - but other files with spaces in the name don't throw the exception. Not only that, the files that don't download using Download2File DO download when selected by OpenDocument.

Quote from: Jurassic Pork
have you tried to replace  the space characters with %20 in your string url request ?
No - because many other files (50+) don't raise the exception and I can get around the problem using an underscore.

Quote from: GAN
May you use TRIM function to avoid spaces at the begin and end of the string.
I could but I know that there are none.

I've come to a 'half-way-house' solution for now.
Upon getting an exception, I write the filename and StatusCode to a memo (visible to the user) and on FormClose I check to see if there has been an error, in which case I post a message and write a text file from the memo. I then Open that file (NotePad) and invoke their e-mail client with my address and subject title. At least I will be made aware of issues and can look at how to address them.

I can't yet find a way to automatically attach the text file to the e-mail so have to rely on the user to do that.  If anyone can give me guidance on attaching files to e-mails I'd be grateful  ;D
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Reading a text file from a web-site.
« Reply #62 on: January 04, 2018, 10:55:17 pm »
I can't yet find a way to automatically attach the text file to the e-mail so have to rely on the user to do that.  If anyone can give me guidance on attaching files to e-mails I'd be grateful  ;D
Luckily I just had this problem yesterday, solved in the same day. Assuming you can send raw email, the message format is as follows:
Code: [Select]
From: you know
To: you know, too
Cc: remove if not needed
Bcc: same
Subject: you know as well
Content-Type: multipart/mixed; boundary="outerboundary"

--outerboundary
Content-Type: multipart/alternative; boundary="innerboundary"

--innerboundary

Content-Type: text/plain; charset="UTF-8"

This is where your plain text message goes

--innerboundary

Content-Type: text/html; charset="UTF-8"

<html><body><p>This is where your HTML formatted message goes</p></body></html>

--innerboundary--

--outerboundary
Content-Type: application/octet-stream; name="name of the file"
Content-Disposition: attachment; filename="name of the file"
Content-Transfer-Encoding: base64

Base64 encoded content of the attachment, of course it doesn't have to be in this format.
Plain text is OK, just remove the Content-Transfer-Encoding header if you do.
Base64 is useful for binary attachment like images.

--outerboundary

--outerboundary--

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #63 on: January 05, 2018, 01:56:07 am »
Quote from: Leledumbo
Quote from: J-G
If anyone can give me guidance on attaching files to e-mails I'd be grateful  ;D
Assuming you can send raw email, the message format is as follows:

By 'raw e-mail' I assume you mean directly from my program rather than invoking the user's e-mail client via 'Mailto:...'

In which case,   no  I haven't mastered that yet   -   not even considered it. I presume that would need some method of executing the SMTP client directly and would certainly be useful, but I have no idea how to even start to think about devising such a method.

With 'MailTo:...'  I can certainly set the address and subject text and it would be useful if I could complete the body with the generated text rather than adding an attachment. That would give the user a simple [Click to send] option.

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

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Reading a text file from a web-site.
« Reply #64 on: January 05, 2018, 02:12:17 am »
By 'raw e-mail' I assume you mean directly from my program rather than invoking the user's e-mail client via 'Mailto:...'

In which case,   no  I haven't mastered that yet   -   not even considered it. I presume that would need some method of executing the SMTP client directly and would certainly be useful, but I have no idea how to even start to think about devising such a method.
Yes, haha...
With 'MailTo:...'  I can certainly set the address and subject text and it would be useful if I could complete the body with the generated text rather than adding an attachment. That would give the user a simple [Click to send] option.
mailto: doesn't allow attachment unfortunately, that's just limitation of the protocol (but could be a security hole if allowed).

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #65 on: January 05, 2018, 04:28:45 pm »
WELL !!

What a turn up  -  I've found the problem :)

It is totally illogical  -  and totally repeatable...

Download2File doesn't like the Capital H preceded by a space !! 

How weird it that ?

I've removed any ' H' from the file names - even replacing them with ' h' - and don't get the exception. Further, adding ' H' to any filename that previously didn't throw the exception now does!

Can anyone throw any light as to why ANY function would not accept a ' H' in an argument?

(@Molly - Since my PM I've refined the issue to ' H' )


EDIT :
Of course it's not Download2File that doesn't like ' H' but the procedure or function 'Get' in fphttpclient  -  and I can't debug into that since I can't find the actual code - just the header declaration in the [Public] section : viz.
Code: Pascal  [Select][+][-]
  1.     // Execute GET on server, store result in Stream, File, StringList or string
  2.     Procedure Get(Const AURL : String; Stream : TStream);
  3.     Procedure Get(Const AURL : String; const LocalFileName : String);
  4.     Procedure Get(Const AURL : String; Response : TStrings);
  5.     Function Get(Const AURL : String) : String;
  6.  

As I am passing the URL and a 'LocalFileName' I assume that the second declaration is pertinent but beyond that I have no idea what 'Get' actually does.

« Last Edit: January 05, 2018, 05:20:47 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Reading a text file from a web-site.
« Reply #66 on: January 05, 2018, 05:50:50 pm »
Of course it's not Download2File that doesn't like ' H' but the procedure or function 'Get' in fphttpclient  -  and I can't debug into that since I can't find the actual code - just the header declaration in the [Public] section : viz.
press ctrl+shift+down arrow to jump between declaration and implementation.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 74
Re: Reading a text file from a web-site.
« Reply #67 on: January 05, 2018, 06:33:08 pm »
You can debug this if you append the path where the fphttpclient unit resides to Project options->Compiler Options->Paths->Other unit files (-Fu). In a windows installation with standard paths this is 'C:\lazarus\fpc\3.0.4\source\packages\fcl-web\src\base' (for FPC 3.0.4). For me a suspicious function is ParseURI from the unit uriparser whose path ('C:\lazarus\fpc\3.0.4\source\packages\fcl-base\src') is to append also.

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #68 on: January 05, 2018, 07:17:23 pm »
Quote from: taazz
press ctrl+shift+down arrow to jump between declaration and implementation.
Thanks taazz  -  I was using 'Find' with argument 'Get' and there were no results. Now I've used this I see that the 'get' is   'hidden' in   TFPCustomHTTPClient.Get(...)' so at least I now have something more to investigate.

Thanks also Lutz  -  I've found that function so I can also put a breakpoint in there. A quick look doesn't reveal any reference to 'H' but I'll see what it does on a 'step through'.

I have a commitment for the evening and I ought to have left 5 minutes ago so it'll have to wait 'til I return   ::)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

madref

  • Hero Member
  • *****
  • Posts: 1104
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Reading a text file from a web-site.
« Reply #69 on: January 05, 2018, 08:59:04 pm »
Quote from: Leledumbo
Quote from: J-G
If anyone can give me guidance on attaching files to e-mails I'd be grateful  ;D
Assuming you can send raw email, the message format is as follows:
i use
Code: Pascal  [Select][+][-]
  1. uses MacOSAll, mimemess, mimepart;
  2.  
  3.  
  4.  
  5. function VerzendMailMetBijlage (Const MailAan, Onderwerp: String; Inhoud: TStrings; Bijlage: String): Boolean;
  6. Var
  7.   Mime : TMimeMess;
  8.   P : TMimePart;
  9.   B : Boolean;
  10. begin
  11.   Mime:=TMimeMess.Create;
  12.   try
  13.     // Zet enkele headers van het bericht.
  14.     Mime.Header.ToList.Text := MailAan;
  15.     Mime.Header.ReplyTo := SenderName + '<' + SenderEMail + '>';
  16.     Mime.Header.Subject := Onderwerp;
  17.     Mime.Header.From := SenderName + '<' + SenderEMail + '>';
  18.     // Maak een multipart deel aan.
  19.     P := Mime.AddPartMultipart('mixed',Nil);
  20.     // Als eerste deel de tekst van de mail toevoegen.
  21.     Mime.AddPartHTML (Inhoud,P);
  22.     // Alle bijlagen toevoegen.
  23.     Mime.AddPartBinaryFromFile (Bijlage,P);
  24.     // Na deze code is het MIME bericht klaar. Het bericht dat verzonden
  25.     // moet worden, kan nu samengesteld worden door EncodeMessage op te roepen,
  26.     // en met SendToRaw te versturen:
  27.     // Bericht samenstellen
  28.     Mime.EncodeMessage;
  29.     // En verzenden met SendToRaw
  30.     B := SendToRaw(SenderName + '<' + SenderEMail + '>', MailAan,
  31.               smtpHost, Mime.Lines, smtpUser, smtpPassword);
  32.     VerzendMailMetBijlage := B;
  33.   finally
  34.     Mime.Free;
  35.   end;
  36. end;     // VerzendMailMetBijlage
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sequoaia 15.5
Lazarus 4.99 (rev main_4_99-2082-gab449f63d6) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #70 on: January 06, 2018, 01:18:17 am »
You can debug this if you append the path where the fphttpclient unit resides to Project options->Compiler Options->Paths->Other unit files (-Fu). In a windows installation with standard paths this is 'C:\lazarus\fpc\3.0.4\source\packages\fcl-web\src\base' (for FPC 3.0.4). For me a suspicious function is ParseURI from the unit uriparser whose path ('C:\lazarus\fpc\3.0.4\source\packages\fcl-base\src') is to append also.

Adding these paths (with suitable adjustment for my setup using FPC 3.0.0) has caused a serious hiatus !

I can no longer compile the project due to unit [Interfaces] - which I don't have in my uses clause so have to assume that it is declared in another unit - causing a fatal error :-

Cannot find LazUTF8 used by Interfaces, incompatible ppu=T:\lazarus\components\lazutils\lib\i386-win32\lazutf8.ppu, package LazUtils

Even removing the paths doesn't restore the situation and a right click on the tab (any tab) - which normally offers me the option to close the unit puts up a message "List index (9) out of bounds"

I'm sure there is going to be a simple way to restore the status quo but I have no idea what it might be  :(

« Last Edit: January 06, 2018, 01:21:09 am by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 74
Re: Reading a text file from a web-site.
« Reply #71 on: January 06, 2018, 01:31:55 am »
Have you compiled with cleanup und build?

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Reading a text file from a web-site.
« Reply #72 on: January 06, 2018, 02:01:44 am »
Have you compiled with cleanup und build?

I have now  :)   -   I've never had to use that facility, a simple [f9] (Run) has always been sufficient. I like the simple life !

At least I'm back able to compile and debug again. Whether I should reinstate those paths and try again is debatable  :o

.... and the "out of bounds" error has also gone away since I used [Ctrl][f4] to close most of the open units.
« Last Edit: January 06, 2018, 02:13:37 am by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018