Recent

Author Topic: [SOLVED] HTTPS GET request with TFPCustomHTTPClient  (Read 5858 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] HTTPS GET request with TFPCustomHTTPClient
« on: July 14, 2017, 03:12:14 pm »
hi All,
i am trying to make a HTTPS GET request but i am missing something.
i get a connection to <IP>:443 could not be established message.
the code is attached.

i am using https://requestb.in for debug.

please help!

using libeay32.dll, ssleay32.dll and Lazarus 1.6.4 64 bit on Windows 7. 
« Last Edit: July 14, 2017, 03:42:54 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: HTTPS GET request with TFPCustomHTTPClient
« Reply #1 on: July 14, 2017, 03:18:08 pm »
What versions of libeay32.dll and ssleay32.dll did you use?
Are you absolutely sure you used the 64 bit versions (because your program is also 64 bit)?

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: HTTPS GET request with TFPCustomHTTPClient
« Reply #2 on: July 14, 2017, 03:26:29 pm »
sorry rvk. i used 64 bit but an older version (0.9.8 ).

when using https://indy.fulgan.com/SSL/openssl-1.0.2l-x64_86-win64.zip i do not get issues in the GET but i have a access violation in

Code: Pascal  [Select][+][-]
  1.   g:= THTTPS.Create(nil);
  2.   g.HTTP_Get(url, r, err);
  3.  
  4.   FreeAndNil(g);  //i get the exception
  5.  

please advise on this exception - would i need a special destructor?
Lazarus 2.0.2 64b on Debian LXDE 10

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: HTTPS GET request with TFPCustomHTTPClient
« Reply #3 on: July 14, 2017, 03:35:31 pm »
That's because you are doing something really strange in your HTTPS-component:

Look at the highlighted line.
Code: Pascal  [Select][+][-]
  1. function THTTPS.HTTP_Get(AURL: string; var AResponse: string; var AError: string; CertifPath: string = ''): boolean;
  2. begin
  3.   Result := False;
  4.   with THTTPS.Create(nil) do
  5.   begin
  6.     _ClientCertificate := CertifPath;
  7.     if CertifPath <> '' then
  8.       OnGetSocketHandler := @SSLClientCertSetup;
  9.     try
  10.       AResponse := Get(AURL);
  11.       Result := True;
  12.     except
  13.       on E: Exception do
  14.       begin
  15.         AError := E.ClassName + '/' + E.Message;
  16.       end;
  17.     end;
  18.     FreeAndNil(self);
  19.   end;
  20. end;

You are releasing the THTTPS itself. Not the instance you created above.
But why are you creating a duplicate of THTTPS in that function?

You didn't define HTTP_Get() as class function so there is no need to recreate a THTTPS-instance at that point because you are already IN such an instance.

So this should work:
Code: Pascal  [Select][+][-]
  1. function THTTPS.HTTP_Get(AURL: string; var AResponse: string; var AError: string; CertifPath: string = ''): boolean;
  2. begin
  3.   Result := False;
  4.   _ClientCertificate := CertifPath;
  5.   if CertifPath <> '' then
  6.     OnGetSocketHandler := @SSLClientCertSetup;
  7.   try
  8.     AResponse := Get(AURL);
  9.     Result := True;
  10.   except
  11.     on E: Exception do
  12.     begin
  13.       AError := E.ClassName + '/' + E.Message;
  14.     end;
  15.   end;
  16. end;

Also, I would wrap the creation of THTTPS in a try/finally
Code: Pascal  [Select][+][-]
  1.   g:= THTTPS.Create(nil);
  2.   try
  3.     g.HTTP_Get(url, r, err);
  4.   finally
  5.     g.Free;
  6.   end;

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: HTTPS GET request with TFPCustomHTTPClient
« Reply #4 on: July 14, 2017, 03:42:37 pm »
thank you.
working like a charm now.

i have uploaded the final version maybe it is helpful to others.
Lazarus 2.0.2 64b on Debian LXDE 10

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] HTTPS GET request with TFPCustomHTTPClient
« Reply #5 on: July 14, 2017, 03:47:20 pm »
Congratulations and thank you tudi_x.

Just for the  record: you are not required to manually use/set certificates that way. The httpget example from fcl-web package works out of the box with your URL (provided that you have the correct ssl libraries installed  :P).

.. and in case you need to do manually, then here is a small snippet in the wiki.

Edit: thank you for the extended corrections rvk. Dumb question as i seem unable to find myself. Can httpclient do a head request ?
« Last Edit: July 14, 2017, 03:52:53 pm by molly »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: [SOLVED] HTTPS GET request with TFPCustomHTTPClient
« Reply #6 on: July 14, 2017, 04:19:19 pm »
Can httpclient do a head request ?
Yes,
Code: Pascal  [Select][+][-]
  1. class procedure TFPCustomHTTPClient.Head(AURL: String; Headers: TStrings);

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] HTTPS GET request with TFPCustomHTTPClient
« Reply #7 on: July 14, 2017, 04:44:26 pm »
Excellent rvk. Thank you very much.

No idea why i overlooked the method, sorry for that :-[

It's a bit of hit and miss though as it's a class function (with a fairly restricted implementation).

I ended up needing to use HTTPMethod, because my test url redirected. It's an interesting approach  :)

 

TinyPortal © 2005-2018