* * *

Author Topic: get webpage content  (Read 1497 times)

apeoperaio

  • Full Member
  • ***
  • Posts: 127
get webpage content
« on: December 11, 2017, 06:06:36 pm »
I am trying to get a webpage content. I used in the past both
TFPHTTPClient from fphttpclient and THTTPSend from synapse httpsend.

Anyway I have some issues.
I am trying to resolve url like:
https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/CCNCC/cids

I I use TFPHTTPClient I am able to download the url content:
urlrequest:= TFPHTTPClient.Create(nil);
try
inputstr:= 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/CCNCC/cids';
urlrequest.SimpleGet(inputstr);
finally
end;

but if the webpage does not exists or if I get some messages I am not able to catch the exceptions (an error is raised by my application even if the request is under try finaly).

Using THTTPSend  I am not able to resolve the url:
httpClient:= THTTPSend.Create;
try
    inputstr:= 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/CCNCC/cids';
    getResult:= httpClient.HTTPMethod('GET', inputstr);
finally
end;

getresult is always false, even if the url is correct.
What am I doing wrong?




molly

  • Hero Member
  • *****
  • Posts: 2345
Re: get webpage content
« Reply #1 on: December 11, 2017, 06:29:53 pm »
see this thread for a recent discussion on the subject.
« Last Edit: December 11, 2017, 06:31:25 pm by molly »

avra

  • Hero Member
  • *****
  • Posts: 1366
    • Additional info
Re: get webpage content
« Reply #2 on: December 11, 2017, 11:39:08 pm »
an error is raised by my application even if the request is under try finaly
To catch an exception you need try..except not try..finally.
For more info take a look at http://www.delphibasics.co.uk/RTL.asp?Name=Finally:
Quote
However, the Finally clause does not actually handle any exceptions

You probably want to combine try..finally as outer scope with try..except as inner scope.
Unfortunately there is no single try..except..finally scope yet in FreePascal, which is a little annoying and stays in the way of code readability.
« Last Edit: December 11, 2017, 11:41:14 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Leledumbo

  • Hero Member
  • *****
  • Posts: 7958
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: get webpage content
« Reply #3 on: December 12, 2017, 06:49:01 am »
You don't always have to use SimpleGet, as it has some predetermined values set, including allowed status code, which is only 200 by default, everything else will raise exception. Set up the properties and parameters yourself and use Get, so you can handle it yourself. Otherwise, what avra suggested should work.

apeoperaio

  • Full Member
  • ***
  • Posts: 127
[Solved] Re: get webpage content
« Reply #4 on: December 12, 2017, 09:54:31 am »
To catch an exception you need try..except not try..finally.

Thanks!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus