如何使用TIdMultiPartFormDataStream处理TIdHTTPServer [英] How to handle TIdHTTPServer with TIdMultiPartFormDataStream
问题描述
我需要有关如何使用Indy的IdHttpServer检索参数和数据的帮助.
我的许多应用程序都使用TIdMultiPartFormDataStream通过php发送数据.由于某些原因,我想使用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;
我怎么知道客户端应用程序是否传递TIdMultiPartFormDataData类型的参数?
在 Embarcadero 和 Indy 论坛.请搜索其存档以及其他存档,例如 Google网上论坛,以查找代码示例.
简而言之,当触发TIdHTTPServer.OnCommandGet
事件时,如果AResponseInfo.ContentType
属性显示为multipart/form-data
(您正在使用的TIdHTTP.Post()
版本将发送application/x-www-form-urlencoded
),则AResponseInfo.PostStream
属性将包含客户端发布的原始MIME数据.您可以使用TIdMessageDecoderMIME
类进行解析.但是,该类从未打算在服务器端使用,因此使用起来不是很直观,但是还是有可能的.
在Indy 11中,我计划直接在TIdHTTPServer
本身中实现本机multipart/form-data
解析,但是由于我们尚未开始在Indy 11上进行工作,因此还没有ETA.
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?
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.
这篇关于如何使用TIdMultiPartFormDataStream处理TIdHTTPServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!