如何显示收到的HTTP Post消息?

时间:2017-02-19 04:14:56

标签: delphi http-post indy

我想将HTTP Post消息发送到我的Delphi应用程序。 因此,当收到HTTP Post消息时,正文应添加到备忘录中。

有人可以发布代码示例吗?

1 个答案:

答案 0 :(得分:2)

当您说“正文”时,我会假设您指的是POST数据或POST请求中发送的内容;不发送html页面正文标记的实际内容。

无论如何,这是我认为你正在寻找的一个快速而肮脏的小例子。编译完成后,运行应用程序,按下开始按钮,然后在同一台机器上打开浏览器,浏览到“http://localhost/”(应拉出小测试表单网页)。然后在2个编辑字段中输入一些数据,并按下小网页上的“发送”按钮。 POSTed内容应出现在应用程序表单上的备忘录中。

主表格单位代码:

单位Unit1;

<div id="FirsRowImg">
  <div class="ImgCell">
    <a class="linkopacity" href="services_backdrops.html" >
      <img src="https://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg" border="0" >
      <p>Backdrops &amp; Drapes</p>
    </a>
  </div>
  <div class="ImgCell">
    <a class="linkopacity" href="services_balloons.html" >
      <img src="https://static-cdn.jtvnw.net/ttv-boxart/For%20Honor-272x380.jpg" border="0" >
      <p>Balloons</p>
    </a>
  </div>
...etc etc...
</div>

和DFM:

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.

和带有测试表单的小HTML页面。将这个小html文件保存在您想要的任何地方,但您必须更改第62行以匹配存储它的路径。

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