Replies: 4 comments
-
Here is an example how to send-and-receive JSON UTF8 strings with REST: function HttpPostGetJSON(const URL: string; var JSON: UTF8String; const _TimeOut: integer = 5000): Boolean;
const
CRLF : UTF8String = #13#10;
var
HTTP : THTTPSend;
Data : TStringStream;
begin
HTTP := THTTPSend.Create;
Data := TStringStream.Create(JSON, TEncoding.UTF8);
JSON := '';
HTTP.Timeout := _TimeOut;
try
HTTP.Headers.Add('Content-Type: application/json; charset=UTF-8') ;
HTTP.Headers.Add('Accept: application/json') ;
Data.Position := 0;
HTTP.Document.CopyFrom( Data, 0);
Data.Size := 0;
Result := HTTP.HTTPMethod('POST', URL);
if Result then begin
if HTTP.Document.Size > 0 then begin
Data.LoadFromStream( HTTP.Document );
JSON := Data.DataString + CRLF + 'Result=' + HTTP.ResultString + CRLF+ 'Protocol='+HTTP.Protocol ;
end;
end;
finally
HTTP.Free;
Data.Free;
end;
end;... maybe adding an extra: try.. except would make it even safer. |
Beta Was this translation helpful? Give feedback.
-
|
Hello, i tried to write a "example" that shows a simple TCP Server and Client "chatting" with each other, it is stored here btw. the component itself is here it uses TThread class to decouple the TTCPBlockSocket component from the LCL Thread. Procedure TClientThread.Execute;
Var
i: Integer;
Begin
// Setup:
// Nichts zu tun der Erzeuger dieses Threads hat
// FOwner, FSocket bereits korrekt initialisiert ;)
FConnection := TSynapseConnection.Create(FOwner, FSocket, fID);
fWasConnected := false;
// Optional: SSL-Handschake
Case FOwner.fMode Of
mServer: Begin
If FOwner.UseSSL Then Begin
FSocket.SSL.CertificateFile := FOwner.SSLCertFile;
FSocket.SSL.PrivateKeyFile := FOwner.SSLKeyFile;
// Die SSL Verbindung aufbauen
If Not FSocket.SSLAcceptConnection Then Begin
// Wenns nicht geklappt hat werfen wie den Client wieder raus befor es überhaupt erst los gehen kann
Synchronize(@RemoveClient);
exit;
End;
End;
End;
mClient: Begin
If FOwner.SSLVerify Then Begin
FSocket.SSL.CertCAFile := FOwner.SSLCAFile; // optional
FSocket.SSLDoConnect(); // Der Client will verbinden
End;
End;
End;
If FSocket.SSL.LastError <> 0 Then Begin
Synchronize(@SSLErrorClient);
Terminate;
exit;
End;
If FSocket.LastError <> 0 Then Begin
Synchronize(@ErrorClient);
Terminate;
exit;
End;
fWasConnected := true;
// Der Server hat einen neuen Client akzeptiert
If FOwner.fMode = mServer Then Begin
If FOwner.UseSSL Then Begin
// Ohne dieses Byte hängt der Client nach dem verbinden bis der Server endlich etwas sendet :(
// Aber mit, empfängt jeder andere client (z.B. L-Net) der Funktioniert ein Byte mit dem er nichts anfangen kann :/
//i := 0;
//FSocket.SendBuffer(@i, 1);
End;
Synchronize(@AcceptClient);
End;
// Wir sind erfolgreich mit dem Server verbunden
If FOwner.fMode = mClient Then Begin
Synchronize(@ConnectClient);
End;
// Execute
While Not Terminated Do Begin
If (FSocket <> Nil) And FSocket.CanRead(1) Then Begin
If (FSocket = Nil) Or (FSocket.PeekBuffer(@i, 1) = 0) Then Begin
Synchronize(@RemoveClient);
End
Else Begin
Synchronize(@ReadData);
End;
End;
If assigned(FSocket) And (FSocket.LastError <> 0) Then Begin
Synchronize(@ErrorClient);
Terminate;
End;
End;
// Teardown
If assigned(FOwner) Then Begin
Synchronize(@RemoveClient);
End;
End; |
Beta Was this translation helpful? Give feedback.
-
|
Also note that there are lots of examples here: Also... there used to be a demo directory somewhere (I have it downloaded) with lots of examples. I'm not sure where I got that one from but it's an official one (from an echo to a TFTPServer folder, total 17 examples). Maybe this was from the old download on http://svn.code.sf.net/p/synalist/code/ but it's not there anymore. Anybody know where it's gone to? |
Beta Was this translation helpful? Give feedback.
-
|
I just searched for EchoSrv.dpr on github (it's one of the files from the demos folder) and found a copy here. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions