如何显示收到的 HTTP Post 消息?
How to display received HTTP Post messages?
我想向我的 Delphi 应用程序发送 HTTP Post 消息。
因此,当收到 HTTP Post 消息时,正文应添加到备忘录中。
任何人都可以 post 代码示例吗?
当您说 "body text" 时,我假设您指的是 POST 数据或在 POST 请求中发送的内容; html 页面正文标记的实际内容未发送。
无论如何,这是我认为您正在寻找的一个快速而粗略的小例子。编译后,运行 应用程序,按下开始按钮,然后在同一台机器上打开浏览器,浏览到“http://localhost/”(应该拉出小测试表单网页)。然后在 2 个编辑字段中输入一些数据并按下小网页上的 "Send" 按钮。 POSTed 内容应出现在应用程序形式的备忘录中。
主窗体单元代码:
单位 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.
和 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
和带有测试表单的 HTML 小页面。将这个小 html 文件保存到任何你想要的地方,但你必须更改第 62 行以匹配你存储它的路径。
<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>
我想向我的 Delphi 应用程序发送 HTTP Post 消息。 因此,当收到 HTTP Post 消息时,正文应添加到备忘录中。
任何人都可以 post 代码示例吗?
当您说 "body text" 时,我假设您指的是 POST 数据或在 POST 请求中发送的内容; html 页面正文标记的实际内容未发送。
无论如何,这是我认为您正在寻找的一个快速而粗略的小例子。编译后,运行 应用程序,按下开始按钮,然后在同一台机器上打开浏览器,浏览到“http://localhost/”(应该拉出小测试表单网页)。然后在 2 个编辑字段中输入一些数据并按下小网页上的 "Send" 按钮。 POSTed 内容应出现在应用程序形式的备忘录中。
主窗体单元代码:
单位 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.
和 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
和带有测试表单的 HTML 小页面。将这个小 html 文件保存到任何你想要的地方,但你必须更改第 62 行以匹配你存储它的路径。
<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>