Recent

Author Topic: LAMW and TFPHTTPClient  (Read 1290 times)

bobkos

  • New Member
  • *
  • Posts: 32
    • Personal Finance Manager
LAMW and TFPHTTPClient
« on: March 06, 2020, 10:04:35 am »
Hi everyone,

I'm stuck a bit, and hope somebody can assist me. Thanks in advance.

I need to get JSON doc from remote server via HTTPS, so I've prepared simple code to get it which is working good on my Windows running PC, nevertheless when I'm trying to get it work on my Android application it's breaking with exception "Hostname resolution for '%hostname%' failed". My assumption that the problem is in HTTPS connection itself, while HTTP execution not leading to error. So my question, is there any way to get it work.

Code: Pascal  [Select][+][-]
  1. uses
  2.   fphttpclient, fpopenssl, openssl;
  3.  
  4. ...
  5.  
  6. function GetReceipt:string;
  7. var
  8.   counter,APos:integer;
  9.   AStream:TStream;
  10.   AUID:string;
  11. begin
  12.   AUID:='';
  13.   AStream:=TStream.Create;
  14.   with TFPHTTPClient.Create(nil) do
  15.     try
  16.       AddHeader('User-Agent','Mozilla/5.0 (compatible; TestApp)');
  17.       IOTimeout:=3000;
  18.       try
  19.         HTTPMethod('GET','https://consumer.oofd.kz/api/tickets/find-by-url?i=3759879297&f=600300097026&s=5032.0&t=20200304T092700',AStream,[302]);
  20.       except on E:Exception do
  21.         Result:=e.message;
  22.       end;
  23.       for counter:=0 to ResponseHeaders.Count-1 do
  24.         if Pos('Location',ResponseHeaders[counter]) = 0 then Continue else begin
  25.           APos:=Pos('/ticket',ResponseHeaders[counter]);
  26.           AUID:=Copy(ResponseHeaders[counter],APos,Length(ResponseHeaders[counter])-APos+1);
  27.           Break;
  28.         end;
  29.      //AUID should be something like this '/ticket/ce658ee5-4c10-4c33-b9e9-994ec6226099'
  30.       if Pos('/ticket',AUID) > 0 then Result:=Get(Format('%s%s',['https://consumer.oofd.kz/api/tickets',AUID]));
  31.     finally
  32.       FreeAndNil(AStream);
  33.       Free;
  34.     end;
  35. end;
  36.  
  37. ...
  38. begin
  39.   ShowMessage(GetReceipt);
  40. end.
  41.  

While looking for solution I've tried jHTTPClient, but it's seems not suitable for my case, because the requested page redirecting to another which is contain data. And I don't know how to handle it.
« Last Edit: March 06, 2020, 11:00:22 am by bobkos »

bobkos

  • New Member
  • *
  • Posts: 32
    • Personal Finance Manager
Re: LAMW and TFPHTTPClient
« Reply #1 on: March 10, 2020, 06:34:41 am »
So far nobody has any ideas how to retrieve data thru https request (with redirection) on androind? Or atleast how to manage redirection with jhttpclient? :'(

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: LAMW and TFPHTTPClient
« Reply #2 on: March 10, 2020, 06:49:44 am »
You don't mention which FPC version you are using?

Anyway, a number of SSL issues were fixed in trunk (3.3.1) which fixed my issues in FreeBSD after a Linux user pointed me to trunk after it fixed his issues.

bobkos

  • New Member
  • *
  • Posts: 32
    • Personal Finance Manager
Re: LAMW and TFPHTTPClient
« Reply #3 on: March 10, 2020, 01:14:46 pm »
I'm using LAMW (Laz4Android 1.8.0 + FPC 3.0.4)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: LAMW and TFPHTTPClient
« Reply #4 on: March 10, 2020, 01:22:29 pm »
Try FPC trunk (see Installing FPC from source).

bobkos

  • New Member
  • *
  • Posts: 32
    • Personal Finance Manager
Re: LAMW and TFPHTTPClient
« Reply #5 on: March 10, 2020, 03:33:59 pm »
Ok, thank you for your advice, I'll try.

 

TinyPortal © 2005-2018