Recent

Author Topic: [SOLVED] LAMW - jhttpclient login with cookie and ssl  (Read 1265 times)

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
[SOLVED] LAMW - jhttpclient login with cookie and ssl
« on: August 28, 2020, 06:13:55 pm »
Hi people, i have an functional python example code, to login an internal network, but i not found the pascal way using the jhttpclient, this is the python example:
Code: Pascal  [Select][+][-]
  1. def conection_very_data(username, password):
  2.     """
  3.     :param username: Número de teléfono registrado en la plataforma de mi.cubacel.net
  4.     :param password: Contraseña registrada en la plataforma de mi.cubacel.net
  5.     :return: Interpretación de BeautifulSoup del código fuente de la pagina de datos del perfil
  6.     """
  7.     urllib3.disable_warnings()
  8.     r2 = requests.get("https://mi.cubacel.net:8443/login/images/cimg/86.jpg", verify=False,
  9.                       cookies=requests.session().cookies, stream=True)
  10.     data = {
  11.         "language": "es_ES",
  12.         "username": username,
  13.         "password": password,
  14.         uword": "every"
  15.     }
  16.     r3 = requests.post("https://mi.cubacel.net:8443/login/Login", data=data, verify=False, cookies=r2.cookies)
  17.     r5 = requests.get("https://mi.cubacel.net/primary/_-ijqJlSHh", verify=False, cookies=r3.cookies)
  18.     soup = BeautifulSoup(r5.text, "html.parser")
  19.     return soup

and this is my try implementation:
Code: Pascal  [Select][+][-]
  1. var
  2.   cookie:jobject;
  3. begin
  4.   cookie:=jHttpClient1.AddCookie('https://mi.cubacel.net:8443/login/images/cimg/86.jpg','verify','false');
  5.   jHttpClient1.GetAsync('https://mi.cubacel.net:8443/login/images/cimg/86.jpg');
  6.   jhttpclient1.SetCookieAttributeValue(cookie,'language','es_ES');
  7.   jhttpclient1.SetCookieAttributeValue(cookie,'username','user1');
  8.   jhttpclient1.SetCookieAttributeValue(cookie,'password','xxxx');
  9.   jhttpclient1.SetCookieAttributeValue(cookie,'uword','every');
  10.  
  11.  
  12.   jhttpclient1.AddNameValueData('language','es_ES');
  13.   jhttpclient1.AddNameValueData('username','user1');
  14.   jhttpclient1.AddNameValueData('password','xxxx');
  15.   jhttpclient1.AddNameValueData('uword','every');
  16.  
  17.   jhttpclient1.PostNameValueDataAsync('https://mi.cubacel.net:8443/login/Login','language=es_ES&username=user1&password=xxxx&uword=every');
  18.   //ShowMessage(contentsum);
  19.   jhttpclient1.GetAsync('https://mi.cubacel.net/primary/_-ijqJlSHh');
  20. end;
  21.  
the page not respond, any help?
« Last Edit: January 16, 2021, 10:40:12 pm by Segator »
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: LAMW - jhttpclient login with cookie
« Reply #1 on: August 28, 2020, 07:47:57 pm »

Quote
he page not respond, any help?

"jHttpClient1.GetAsync" and "jhttpclient1.PostNameValueDataAsync"  are Asynchronous!

So, you need to handle the corresponding events....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie
« Reply #2 on: August 28, 2020, 08:15:48 pm »
Hi jmpessoa,
Quote
"jHttpClient1.GetAsync" and "jhttpclient1.PostNameValueDataAsync"  are Asynchronous!

So, you need to handle the corresponding events....

yes i manage that, the problem is this:
Code: Pascal  [Select][+][-]
  1. r2 = requests.get("https://mi.cubacel.net:8443/login/images/cimg/86.jpg", verify=False,
  2.                       cookies=requests.session().cookies, stream=True)
  3.     data = {
  4.         "language": "es_ES",
  5.         "username": username,
  6.         "password": password,
  7.         uword": "every"
  8.     }
  9.     r3 = requests.post("https://mi.cubacel.net:8443/login/Login", data=data, verify=False, cookies=r2.cookies)
  10.     r5 = requests.get("https://mi.cubacel.net/primary/_-ijqJlSHh", verify=False, cookies=r3.cookies)


r2 is a request to a captcha imagen  that save the cookies in some where to make a post request (r3) with that cookie, context and data.

r5 is a get with the r3 cookies as parameter and with the loggin done.

the cuestion es how i do this with jhttpclient? my try responce with "" content
« Last Edit: August 28, 2020, 08:28:02 pm by Segator »
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: LAMW - jhttpclient login with cookie
« Reply #3 on: August 28, 2020, 08:50:02 pm »

But

Quote
r2 = requests.get(................

Is synchronous  logic..... so, change the LAMW code to  synchronous  methods, too.

Or refactore your code for Asynchronous logic....  [not sequential line after line ....]
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie
« Reply #4 on: August 28, 2020, 09:13:23 pm »
yes you are right, when you call getasync the above code have not effect over that method because it has move to other threads i think, i am go to make some changes and find a public webpage to logging with jhttpclient using coockies for you can do tests
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie
« Reply #5 on: August 29, 2020, 03:52:22 pm »
I make some progress, this code work:
Code: Pascal  [Select][+][-]
  1. jWebView1.Navigate('https://mi.cubacel.net/primary/_-ijqJlSHh');
  2.   jHttpClient1.ClearCookieStore();
  3.  
  4.   cookie:=jHttpClient1.AddCookie('https://mi.cubacel.net:8443/login/images/cimg/86.jpg','verify','false');
  5.  
  6.   jHttpClient1.GetStateFul('https://mi.cubacel.net:8443/login/images/cimg/86.jpg');
  7.  
  8.   jHttpClient1.AddNameValueData('language', 'es_ES');
  9.   jHttpClient1.AddNameValueData('username', 'xxxx');
  10.   jHttpClient1.AddNameValueData('password', 'xxxx');
  11.   jHttpClient1.AddNameValueData('uword', 'every');
  12.   ShowMessage(jHttpClient1.PostStateful('https://mi.cubacel.net:8443/login/Login'));
  13.  
  14.   jetdataformat.text:=jhttpclient1.Get('https://mi.cubacel.net/primary/_-ijqJlSHh');
  15.   /

but this is a synchrony code and the app is freezing some time, i try to use jAsyncTask component but the app crash (i removed the toast and the interface interaction of course), the other problem is an strange thing, if i not open the page firt with a webview the jhttpclient response with '' string, is a ssl problem maybe i add openssl to the uses clause, but one problem at a time 😀, so any asynchronous solution like this?
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: LAMW - jhttpclient login with cookie
« Reply #6 on: August 29, 2020, 08:23:51 pm »

try change the last line to:

jetdataformat.text:=jhttpclient1.GetAsync('https://mi.cubacel.net/primary/_-ijqJlSHh');

and handle the result in the event....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie
« Reply #7 on: August 30, 2020, 03:01:56 am »
Yes that solution work 👍, now the other rare problem, jhttpclient not open an https sites, but if you open that sites first with a webview then jhttpclient can downloaded the html, i add the openssl to the uses but i can't use jhttpclient without webview to browse https sites?, any suggestions or solution?.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: LAMW - jhttpclient login with cookie
« Reply #8 on: August 30, 2020, 03:24:05 am »

Indeed, jhttpclient don't  have [yet] a good support to https .... sorry.
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie
« Reply #9 on: August 30, 2020, 04:31:28 pm »
but we can try to improved it, i see other thread with https or ssl problem and  the people copy a openssl arm build to the folder library, but the system (android) have the own ssl library, must by a way to use it with out using an external it's the ssl library the problem?.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: LAMW - jhttpclient login with cookie
« Reply #10 on: August 30, 2020, 06:42:16 pm »
Quote
the people copy a openssl arm build to the folder library...

Yes, a pure Free Pascal  using "FPHttpClient.pas"  solution....

Quote
but the system (android) have the own ssl library....

this is what we need!


Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: LAMW - jhttpclient login with cookie [SOLVED]
« Reply #11 on: January 16, 2021, 10:39:12 pm »
Finally i solve the jhttpclient ssl problem with untrusted certificates, i add a new function to trust all certificates, use this if you really trust in the URL for security reasons, just wait the @jmpessoa approval in the github code and update it.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

jmpessoa

  • Hero Member
  • *****
  • Posts: 1787
Re: [SOLVED] LAMW - jhttpclient login with cookie and ssl
« Reply #12 on: January 17, 2021, 02:55:23 pm »

Done!

Thank you!

[Help] What about "put" and "delete"?   [so we can support RestFull Apis....]
You can try mimic  the "get" [or "post"]  method code...
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 159
    • https://github.com/Nenirey
Re: [SOLVED] LAMW - jhttpclient login with cookie and ssl
« Reply #13 on: January 17, 2021, 03:26:59 pm »
I make some search an maybe is simple as change the method name like client3.setRequestMethod("GET") to client3.setRequestMethod("PUT") or DELETE, maybe is look like better a function to change this with the same mimic as "setRequestMethod" in jhttpclient.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018