Recent

Author Topic: [SOLVED] LAMW. jHttpClient doesn't work with https protocol  (Read 10447 times)

jluixjurado

  • New Member
  • *
  • Posts: 21
[SOLVED] LAMW. jHttpClient doesn't work with https protocol
« on: September 28, 2022, 05:04:37 pm »
Hello!

I'm trying to reach an url with the https:// prefix using jHttpClient, but with not success so far (it works ok with http://)

I've read on almost all related topic in this forum, and I've put different android libssl.so and libcrypto.so inside /libs/armeabi-v7a folder. I've tried with synapse, ...

In this link https://forum.lazarus.freepascal.org/index.php/topic,46830.15.html the user MrBean uploaded a working example (https://www.dropbox.com/s/5k5cvfxlgumrb69/AppDemoHttpGet.zip), but sadly it's not there anymore.

Please. How can I reach that kind of urls?

Thanks in advance
« Last Edit: September 29, 2022, 05:37:40 pm by jluixjurado »

jluixjurado

  • New Member
  • *
  • Posts: 21
Re: LAMW. jHttpClient doesn't work with https protocol
« Reply #1 on: September 28, 2022, 10:42:44 pm »
After a lot of tries, It seems that the problem is that Synapse doesn't seem to work, I mean, it compiles ok, but then, even for not https, urls, it gets always empty strings. I've read in https://forum.lazarus.freepascal.org/index.php/topic,49443.msg360431.html#msg360431 that there ws a problem with older versions of fpc.exe, but I'm using fpc 3.2.0.

For instance, here are several tests:

  SynHttp := THTTPSend.Create;
  //SynHttp.Sock.CreateWithSSL(TSSLOpenSSL);
  //SynHttp.Sock.SSLDoConnect;
  SynHttp.HTTPMethod('GET', 'http://...'); //+'?id='+id+'&mensaje='+mensaje);
  respuesta := TStringList.create;
  respuesta.LoadFromStream(SynHttp.Document);
  incluyeEnLista('Synapse GET: '+respuesta.Text);

...

  response := TMemoryStream.create;
  try
     HttpPostURL('http://...','id='+id+'&mensaje='+mensaje',response);
     incluyeEnLista('Synapse POST: '+StreamToString(response));
  finally
    response.free;
  end;

...

  respuesta := tStringList.create;
  HttpGetText('http://...',respuesta);
  incluyeEnLista('Synapse 2 '+respuesta.Text);     

The same url, using jHttpClient, works (but not with a https url):

  HttpClient.AddNameValueData('id', id);
  HttpClient.AddNameValueData('mensaje', mensaje);
  //Non-blocking! handled by:  OnCodeResult and OnContentResult
  HttpClient.PostNameValueDataAsync(servidorWeb+urlRecepcionSMS);   


I've recompiled laz_synapse and it seems to localize openssl libraries:
Ventana de Mensajes
Verbose: Selected chip architecture: armeabi-v7a
Verbose: Taking libraries from folder: ...\libs\armeabi-v7a
Verbose: Found library: libcontrols.so
Verbose: Found library: libcrypto.so
Verbose: Found library: libssl.so
Compilar paquete laz_synapse 40.1: Éxito


Please, Could someone of you give me some example or a hint to keep trying?
 

ralphtheprog

  • Newbie
  • Posts: 1
Re: LAMW. jHttpClient doesn't work with https protocol
« Reply #2 on: September 29, 2022, 07:56:00 am »
Hi new around here and idk if this helps but this is something I was playing with to make an https request to get some JSON,

Code: Pascal  [Select][+][-]
  1. unit suppliesMeUnit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, opensslsockets;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Memo1: TMemo;
  19.     procedure dpSurprise(Sender: TObject);
  20.     procedure Label1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.dpSurprise(Sender: TObject);
  37. var
  38.   Client : TFPHttpClient;
  39.   S : String;
  40.  
  41.   begin
  42.   Client := TFPHttpClient.Create(nil);
  43.   Client.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
  44.   Client.AddHeader('Content-type','application/json; charset=UTF-8');
  45.   Client.AddHeader('Accept', 'application/json');
  46.   Client.AllowRedirect := true;
  47.   S := Client.Get(Edit1.Text);
  48.   Memo1.Caption := S;
  49.  
  50. end;
  51.  
  52. procedure TForm1.Label1Click(Sender: TObject);
  53. begin
  54.  
  55. end;
  56.  
  57. end.
  58.  
  59.  

jluixjurado

  • New Member
  • *
  • Posts: 21
Re: LAMW. jHttpClient doesn't work with https protocol
« Reply #3 on: September 29, 2022, 12:15:44 pm »
Thanks for trying to help, ralphtheprog, but I don't think that's the solution.

Indeed, yout approach works on Windows or Linux (I have used fphttpclient unit regularly without problems in those two systems with http and with https). However, in Android it doesn't seem to work. Look my example:

procedure TAndroidModule1.Button1Click(Sender: TObject);
var
  r,a : string;
begin
  //TFPCustomHTTPClient
  r:=EditText1.Text;
  try
     a:=TFPCustomHTTPClient.SimpleFormPost(r,'');
     ListView1.Add('TFPCustomHTTPClient.SimpleFormPost|Request: '+r+'|Response: '+a);
  except
     on e : exception do begin
       ListView1.Add('TFPCustomHTTPClient.SimpleFormPost|Request: '+r+'|Response: '+e.Message);
     end;
  end;
end; 


Even with a valid http URL, it gets:

Response: Host name resolution for "..." failed.

I've tried with LNet too:

procedure TAndroidModule1.Button2Click(Sender: TObject);
var
  HttpClient: TLHTTPClient;
  r, Host, URI, FileName, AltFileName: ansistring;
  Port: Word;
  UseSSL: Boolean;
  SSLSession: TLSSLSession;
begin
  //LNet
  r:=EditText1.Text;
  try
     dummy := THTTPHandler.Create;
     dummy.form := Self;
     UseSSL := DecomposeURL(r, Host, URI, Port);
     HttpClient := TLHTTPClient.Create(nil);
     SSLSession := TLSSLSession.Create(HttpClient);
     SSLSession.SSLActive := UseSSL;
     HttpClient.Session := SSLSession;
     HttpClient.Host := Host;
     HttpClient.Method := hmGet;
     HttpClient.Port := Port;
     HttpClient.URI := URI;
     HttpClient.Timeout := -1;
     HttpClient.OnDisconnect := @dummy.ClientDisconnect;
     HttpClient.OnDoneInput := @dummy.ClientDoneInput;
     HttpClient.OnError := @dummy.ClientError;
     HttpClient.OnInput := @dummy.ClientInput;
     HttpClient.OnProcessHeaders := @dummy.ClientProcessHeaders;
     HttpClient.SendRequest;
  except
     on e : exception do begin
       ListView1.Add('TLHTTPClient|Request: '+r+'|Answer: '+e.Message);
       writeln('TLHTTPClient|Request: '+r+'|Answer: '+e.Message);
     end;
  end;
end; 
                               

It's interesting that, when running the LNet example without putting libcrypto.so and libssl.so in \libs\armeabi-v7a, the code raises the exception Unable to initialize OpenSSL Library, please check your OpenSSL installation. That suggests (as the formerly commented Host name resolution for "..." failed) that the problem is not the openssl libs, but the access to Internet from pascal specific code.

In fact, so far, I've only been able to get a http (not https) response using jHTTPClient.

Here (https://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application) I read that, for https accessing, the AndroidManifest.xml has to have android:usesCleartextTraffic="true" informed inside the Application tag, but I've checked and that line is already there, so I'm running out of ideas.

Without accessing to https urls it's almost impossible to build a modern day regular communication app, that's why I'm investing so much time in this problem. Please, help me!

Thanks in advance.


jluixjurado

  • New Member
  • *
  • Posts: 21
Re: LAMW. jHttpClient doesn't work with https protocol
« Reply #4 on: September 29, 2022, 05:35:13 pm »
Hi again.

I've been able to solve the problem.

First of all, the jHttpClient object WORKS RIGHT WITH HTTPS! :-[ The problem was a weird interaction between my company's firewall and a ssl certificated wrong configured. I'm really sorry.

Secondly, I haven't been able to make any of the pure pascal based NET components that I've tried (TFPHttp, LNET and Synapse) to work in Android. For all of you with time and desire to follow with the effort, I've developed a test program which you can access to here: https://1drv.ms/u/s!AphGvmscOT8qgZh8364yUjNsZoJobQ?e=n4YGw3

As stated in prior messages, when you try:

jHttpClient button -> Almost inmediate Right answer gor http and https requests
TFPHttp button -> after some seconds blocking the app (around 10): Host name resolution for "ave.b...." failed
Synapse -> After around 30 seconds blocking the app: Empty response ('')
LNet -> After around 10 seconds blocking the app -> no response at all

Thanks for your help!

 

TinyPortal © 2005-2018