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

前几天研究了下post的方式和服务器的接收处理,环境delphi12.2.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;

相关推荐
接着奏乐接着舞。4 分钟前
【2026】73道Redis 常见面试题与参考答案
数据库·redis·后端·缓存
我命由我1234516 分钟前
Linux - Linux/POSIX 路径斜杠折叠规则
linux·运维·服务器·android studio·android jetpack·android-studio·android runtime
今日无bug36 分钟前
大模型的回答是怎么一个字一个字蹦出来的?前端流式输出解析
前端·llm·agent
计算机魔术师37 分钟前
Anthropic 复盘 Claude 模型越权访问真实系统事件并改进对齐与安全措施
前端
掘金者阿豪44 分钟前
Seedance 2.0/2.5 虚拟素材能跨 Key 共用吗?一次讲清 Asset ID、账号隔离与 SaaS 素材架构
前端·后端
用户50093768390391 小时前
热敏小票 Web 打印:我在 58/80mm 上折腾的两周
前端·vue.js
XGM1 小时前
一个前端新手的"顿悟"时刻:DOM树、渲染原理和JS动态渲染
前端
Uncertainty!!1 小时前
Linux 主机无法完成校园网认证排查记录:从 DHCP、ARP 到 Portal 认证流程分析
linux·运维·服务器
东方小月1 小时前
一篇文章带你深入拆解Skill的本质与工程实现,让你不再滥用Skill
前端·人工智能·后端
其实防守也摸鱼1 小时前
权限提升与横向移动:从内网渗透到域控的完整技术图谱
运维·服务器·数据库·安全·github·copilot·渗透