如何使用 TIdMultiPartFormDataStream 处理 TIdHTTPServer

XBa*_*000 5 delphi indy simplehttpserver indy10

您好,我需要有关如何使用 indy 中的 IdHttpServer 检索参数和数据的帮助。

我的许多应用程序使用 TIdMultiPartFormDataStream 通过 php.ini 发送数据。我想使用 TIdHTTPServer 出于某种原因验证参数并将请求转发到其目的地。

我创建了一个简短的示例供您查看。

uses
  IdContext, IdMultipartFormData;

// Server Side------------------------------------------------

IdHTTPServer1.Defaultport := 88;
IdHTTPServer1.active := True;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  // the request will be pass through its destination by POST/GET
  // and send the result back to the client apps.
  AResponseInfo.ContentText := ARequestInfo.Params.Text;
end;

// Client Side------------------------------------------------
// This will work using the standard Post or Get
procedure TForm1.btnPost1Click(Sender: TObject);
var
  sl: TStringList;
  res: String;
begin
  sl := TStringList.Create;
  try
    sl.Add('Param1=Data1');
    sl.Add('Param2=Data1');
    sl.Add('Param3=Data2');
    sl.Add('Param4=Data3');
    res := IdHTTP1.Post('http://localhost:88/some.php', sl);
    ShowMessage(res);
  finally
    sl.Free;
  end;
end;

//how can i get the parameters and value for this code in my IdHttpServer
procedure TForm1.btnPost2Click(Sender: TObject);
var
  mfd: TIdMultiPartFormDataStream;
  res: String;
begin
  mfd := TIdMultiPartFormDataStream.Create;
  try
    mfd.AddFormField('Param1', 'Data1');
    mfd.AddFormField('Param2', 'Data1');
    mfd.AddFormField('Param3', 'Data2');
    mfd.AddFormField('Param4', 'Data3');
    res := IdHTTP1.Post('http://localhost:88/some.php', mfd);
    ShowMessage(res);
  finally
    mfd.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我如何知道客户端应用程序是否传递 TIdMultiPartFormDataStream 类型的参数?

Rem*_*eau 3

这个问题之前已在EmbarcaderoIndy论坛中被多次询问和回答。请搜索他们的档案以及其他档案(例如Google Groups)以查找代码示例。

简而言之,当TIdHTTPServer.OnCommandGet事件被触发时,如果AResponseInfo.ContentType属性显示multipart/form-data(您正在使用的版本TIdHTTP.Post()将改为发送application/x-www-form-urlencoded),则该AResponseInfo.PostStream属性将包含客户端发布的原始 MIME 数据。您可以使用该类TIdMessageDecoderMIME来解析它。然而,该类从未打算在服务器端使用,因此使用起来不是很直观,但仍然是可能的。

在 Indy 11 中,我计划multipart/form-data直接在TIdHTTPServer其自身中实现本机解析,但由于我们尚未开始 Indy 11 的工作,因此还没有预计时间。