How to handle TIdHTTPServer with TIdMultiPartFormDataStream

孤街浪徒 提交于 2019-12-14 01:12:48

问题


Hi i need help on how to retrieved the parameters and data using IdHttpServer from indy.

many of my application uses TIdMultiPartFormDataStream to send data over the php. I would like to use the TIdHTTPServer to verify parameters for some reason and forward the request to its destination.

i created a short example for you to see.

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;

and how would i know if the Client apps pass a TIdMultiPartFormDataStream type of parameter?


回答1:


This has been asked and answered many times before in the Embarcadero and Indy forums. Please search through their archives, as well as other archives, like Google Groups, to find code examples.

In a nutshell, when the TIdHTTPServer.OnCommandGet event is triggered, if the AResponseInfo.ContentType property says multipart/form-data (the version of TIdHTTP.Post() you are using will send application/x-www-form-urlencoded instead), the AResponseInfo.PostStream property will contain the raw MIME data that the client posted. You can use the TIdMessageDecoderMIME class to parse it. However, that class was never intended to be used server-side, so it is not very intuitive to use, but it is possible nontheless.

In Indy 11, I am planning on implementing native multipart/form-data parsing directly into TIdHTTPServer itself, but there is no ETA on that yet as we have not started work on Indy 11 yet.



来源:https://stackoverflow.com/questions/11025703/how-to-handle-tidhttpserver-with-tidmultipartformdatastream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!