delphi webserver post数据与对应的接收方式

前几天研究了下post的方式和服务器的接收处理,环境delphi10.1,首先说客户端post数据,采用idhttp,有两种方式,一种是字符串,一种是流如下:

1.psot字符串的方式

var
  s: string;
  stm: TStringStream; //接收返回内容
  send:TStringList;   //post的内容
begin
  send:=TStringList.Create;

send.Text:=‘要post的内容’;
  //send.add(‘name=张三’); //也可以按照不同的参数赋值
  //send.add(‘sex=男’);
  stm := TStringStream.Create(s, TEncoding.UTF8);

stm.Position := 0;
  try
    IdHTTP1.Post(url, send , stm);
    Memo2.Lines.Text:=stm.DataString; //服务端返回的内容
  except
    result := false;
  end;
  stm.Free;

send.free;

2.通过流的方式post

var
  s,res:string;
  stm:TStringStream;
begin
  s:=‘要post的内容,比如说xml格式的文本’;
  stm := TStringStream.Create(s, TEncoding.UTF8);
  stm.Position := 0;
  try
    res:=IdHTTP1.Post(‘url地址’, stm);
    Memo2.Lines.Text:=res; //服务端返回的内容
  finally
    stm.Free;

end;

下面是服务端接收处理,服务端采用IdHTTPServer,在OnCommandGet事件中处理数据

ARequestInfo.ContentType := ‘text/html;Charset=UTF-8’;
  path := ARequestInfo.Document; //访问的路径
  if UpperCase(ARequestInfo.command) = ‘POST’ then

begin
 //接收post字符串的处理
 // memo1.Lines.Add(arequestinfo.Params.Values[‘name’]) ;//按参数名称接收
// memo1.Lines.Add(arequestinfo.Params.Values[‘sex’]);

//    Memo1.Lines.Add(‘FormParams:’+ARequestInfo.FormParams); //所有数据
//    Memo1.Lines.Add(‘Params:’+ARequestInfo.Params.Text);
//    aRequestContent:= ARequestInfo.UnparsedParams;
    aRequestContent := ARequestInfo.Params.GetText;
   //下面是接收数据流的处理过程

// if (ARequestInfo.PostStream <> nil) and (ARequestInfo.PostStream.Size > 0) then
   // bergin
   //      ARequestInfo.PostStream.Position := 0;

//      aRequestContent := StreamToString(ARequestInfo.PostStream);
   // end;

aRequestContent := tiduri.URLDecode(aRequestContent);  //解决汉字乱码问题
// 数据处理过程

AResponseInfo.ContentType := ‘text/html’;
AResponseInfo.CharSet := ‘utf-8’;
AResponseInfo.ContentText := ‘根据处理过程返回客户端信息’;
AResponseInfo.WriteContent;
————————————————
版权声明:本文为CSDN博主「zflybird」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zflybird/article/details/80202485

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值