Recent

Author Topic: [SOLVED] LNET HTTPClient - how to POST XML  (Read 12392 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
[SOLVED] LNET HTTPClient - how to POST XML
« on: July 28, 2010, 10:43:37 pm »
Hi,

I can't find any example how to POST some data. In demos is only simple example how to POST using params (http//www.somesite.com/php?param1=value1&param2=value2...). But, how can I post this XML like in synapse:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.buttAddDocumentClick(Sender: TObject);
  2. var
  3.   aHTTP: THTTPSend;
  4. begin
  5.   aHTTP := THTTPSend.Create;
  6.   try
  7.     WriteStrToStream(aHTTP.Document, '<document><title>Test document</title><body>Some text</body></document>');
  8.     aHTTP.MimeType := 'application/xml';
  9.  
  10.     if aHTTP.HTTPMethod('POST', 'http://localhost:3000/documents.xml') then
  11.     begin
  12.       aHTTP.Document.Position := 0;
  13.       Memo1.Lines.LoadFromStream(aHTTP.Document);
  14.     end else ShowMessage('Can not POST');
  15.   finally
  16.     aHTTP.Free;
  17.   end;
  18. end;  
  19.  

Regards
« Last Edit: October 23, 2010, 08:46:25 pm by Dibo »

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: LNET HTTPClient - how to POST XML
« Reply #1 on: October 23, 2010, 08:45:23 pm »
Ok, I very much needed this solution, so I analyze procedure TLHTTPClientSocket.SendRequest. By default, http client does not send absolutely no header (only host), so I sniff what sends synapse and do this same by AddExtraHeader procedure. XML is just new text block in document so I add this by AddExtraHeader too. This is solution code which work, maybe someone need this too:

Code: Pascal  [Select][+][-]
  1.   DecomposeURL('h t tp://some.host.com/login.xml', sHost, sUrl, sPort);
  2.   httpclient.Method := hmPost;
  3.   httpclient.Port := sPort;
  4.   httpclient.Host := sHost;
  5.   httpclient.URI := sUrl;
  6.   httpclient.AddExtraHeader('Keep-Alive: 300');
  7.   httpclient.AddExtraHeader('Connection: keep-alive');
  8.   httpclient.AddExtraHeader('Content-Length: 111'); // <-XML length
  9.   httpclient.AddExtraHeader('Content-Type: application/xml; charset=utf-8');
  10.   httpclient.AddExtraHeader(LineEnding);
  11.   httpclient.AddExtraHeader(
  12.     '<?xml version="1.0"?>' + LineEnding +
  13.     '<login>'+ LineEnding +
  14.       'dibo'+ LineEnding +
  15.     '</login>'
  16.   );
  17.   httpclient.SendRequest;

LHTTPClient is a very good socket but lacks some basic features visible for programmer (and PUT, DELETE http methods).
But this is not the end of my problems :P . There is demo how to download file, but there is no how to upload :) . Maybe I'll find a way ...

Regards

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: [SOLVED] LNET HTTPClient - how to POST XML
« Reply #2 on: October 26, 2010, 07:56:49 am »
You'll find a way dibo, your good. :D
This post is helpfull too.

 

TinyPortal © 2005-2018