Forum > Networking and Web Programming
[Solved] ESocketError on Mac
Frogfather:
Hello,
I'm having some difficulty making a GET request using fphttpclient with MacOS 10.15.7 Catalina. I've just upgraded to Lazarus 2.0.12 / fpc 3.2.0.
The code, which is intended to query the public api of an energy supplier, is pretty uncontroversial - and actually works fine on Linux Fedora 28.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function ToctopusForm.queryApi(api: string): string;Var S : string; HTTP : TFPHttpClient;begin result:=''; HTTP := TFPHttpClient.Create(nil); HTTP.AllowRedirect:=True; HTTP.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); HTTP.AddHeader('Content-Type', 'application/json'); try S := HTTP.Get(api); writeln(s); result:=s; finally http.free; end;end;
openssl is installed via homebrew and openssl version -a returns:
OpenSSL 1.1.1h 22 Sep 2020
built on: Sat Oct 10 03:46:20 2020 UTC
platform: darwin64-x86_64-cc
options: bn(64,64) rc4(16x,int) des(int) idea(int) blowfish(ptr)
compiler: clang -fPIC -arch x86_64 -O3 -Wall -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_CPUID_OBJ -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DKECCAK1600_ASM -DRC4_ASM -DMD5_ASM -DAESNI_ASM -DVPAES_ASM -DGHASH_ASM -DECP_NISTZ256_ASM -DX25519_ASM -DPOLY1305_ASM -D_REENTRANT -DNDEBUG
OPENSSLDIR: "/usr/local/etc/openssl@1.1"
ENGINESDIR: "/usr/local/Cellar/openssl@1.1/1.1.1h/lib/engines-1.1"
Seeding source: os-specific
I've also done
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
as advised by homebrew and various blogs
I'm testing with https://jsonplaceholder.typicode.com/todos/1 both this and the actual api I want to use work fine on Linux Fedora 28
The error is thrown in procedure TInetSocket.Connect and is ESocketError.
I'm pretty sure the issue is down to something in the openssl installation but I've tried everything I can think of. :'(
Gustavo 'Gus' Carreno:
Hi FrogFather,
There's been a change in the way fphttpclient handles SSL.
Pre FPC 3.2.0 you would use ssockets, sslsockets, openssl and fpopenssl, now from FPC 3.2.0 onwards you only need to use opensslsockets.
For Pre FPC 3.2.0 you would also need to add this to the OnGetSocketHandler callback for newer openssl libs:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- {Somewhere else}begin InitSSLInterface;// <-- very important pre 3.2.0, not needed post 3.2.0 instanceOffpHTTPClient:= TFPHTTPClient.Create; instanceOffpHTTPClient.OnGetSocketHandler:=@GetSocketHandler;end; procedure TSomethingOrOther.GetSocketHandler(Sender: TObject; const UseSSL: Boolean; out AHandler: TSocketHandler);begin AHandler := TSSLSocketHandler.Create; TSSLSocketHandler(AHandler).SSLType := stTLSv1_2;end;
But this is for Windows/Linux.
I've stumbled on these two threads that mention stuff about macOS:
* https://forum.lazarus.freepascal.org/index.php/topic,34058.msg396680.html#msg396680
* https://forum.lazarus.freepascal.org/index.php/topic,50350.msg374104.html#msg374104
Hope that helps in any way.
Cheers,
Gus
Gustavo 'Gus' Carreno:
Hey FrogFather,
Sorry forgot to link to the example where I had to overcome some SSL issues:
https://github.com/wp-xyz/corona/blob/master/source/cdownloadmanager.pas
Pay special attention to the {$IFDEF} wizardry that I had to pull off.
Hope that gives you more insight.
Cheers,
Gus
Frogfather:
Hi Gus,
That fixed it! Many thanks
Gustavo 'Gus' Carreno:
Hi FrogFather,
--- Quote from: Frogfather on March 08, 2021, 08:04:46 pm ---That fixed it! Many thanks
--- End quote ---
You're quite welcome!!
Can I ask what which of my answer solved your problem?
- The cdownloadmanager.pas
- The links to this forum
Cheers,
Gus
Navigation
[0] Message Index
[#] Next page