I want to send HTTP Post messages to my Delphi application. So, when a HTTP Post message is received, the body text should be added to a memo.
Could anyone post a code sample?
When you say "body text" I'll assume you mean the POST data or the content that was sent in the POST request; The actual content of the html page's body tag isn't sent.
Anyway, here's a quick and dirty little example of what I think you're looking for. Once compiled, run the app, push the start button, then open a browser on the same machine, browse to "http://localhost/" (should pull up the little test form web page). Then enter some data in the 2 edit fields and push the "Send" button on the little web page. The POSTed content should appear in the memo on the form of the app.
The main form unit code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdContext, IdCustomHTTPServer, Vcl.StdCtrls, IdBaseComponent, IdComponent,
IdCustomTCPServer, IdHTTPServer, IdHeaderList, IdGlobal;
type
TForm2 = class(TForm)
IdHTTPServer1: TIdHTTPServer;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
procedure IdHTTPServer1CreatePostStream(AContext: TIdContext; AHeaders: TIdHeaderList; var VPostStream: TStream);
procedure IdHTTPServer1DoneWithPostStream(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; var VCanFree: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
idHTTPServer1.Active := true;
end;
procedure TForm2.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
var
AForm : TStringList;
Stream : TStream;
S : string;
begin
if ARequestInfo.Command = 'POST' then
begin
Stream := ARequestInfo.PostStream;
if assigned(Stream) then
begin
Stream.Position := 0;
S := ReadStringFromStream(Stream);
TThread.Synchronize(nil,
procedure
begin
memo1.Lines.Add(S);
end);
end
end
else
begin
AForm := TStringList.Create;
try
AForm.LoadFromFile('c:\debug\form.html');
AResponseInfo.ContentText := AForm.Text;
finally
AForm.Free
end;
end
end;
procedure TForm2.IdHTTPServer1CreatePostStream(AContext: TIdContext; AHeaders: TIdHeaderList; var VPostStream: TStream);
begin
VPostStream := TMemoryStream.Create;
end;
procedure TForm2.IdHTTPServer1DoneWithPostStream(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; var VCanFree: Boolean);
begin
VCanFree := false;
end;
end.
and the DFM:
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 362
ClientWidth = 666
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 16
Top = 20
Width = 75
Height = 25
Caption = 'Start'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 156
Top = 8
Width = 473
Height = 337
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object IdHTTPServer1: TIdHTTPServer
Bindings = <>
OnCreatePostStream = IdHTTPServer1CreatePostStream
OnDoneWithPostStream = IdHTTPServer1DoneWithPostStream
OnCommandGet = IdHTTPServer1CommandGet
Left = 76
Top = 88
end
end
and the little HTML page with a test form. Save this little html file anywhere you want but you'll have to change line 62 to match the path to where you store it.
<html>
<body>
Test POST<br>
<form id="form1" action="/" method="POST">
input 1: <input id="edit1" name="edit1"><br>
input 2: <input id="edit2" name="edit2"><br>
<button type="submit">Send</button>
</form>
</body>
</html>