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;

相关推荐
你的人类朋友1 小时前
快速搭建redis环境并使用redis客户端进行连接测试
前端·redis·后端
文火冰糖的硅基工坊2 小时前
[创业之路-653]:社会产品与服务的分类
大数据·数据库·人工智能
深蓝电商API2 小时前
实战破解前端渲染:当 Requests 无法获取数据时(Selenium/Playwright 入门)
前端·python·selenium·playwright
235162 小时前
【MySQL】数据库事务深度解析:从四大特性到隔离级别的实现逻辑
java·数据库·后端·mysql·java-ee
脚踏实地的大梦想家2 小时前
【LangChain】P7 对话记忆完全指南:从原理到实战(下)
数据库·langchain
conkl2 小时前
Flask 与 MySQL 数据库集成:完整的 RESTful API 实现指南
数据库·mysql·flask
bestcxx3 小时前
(二十七)、k8s 部署前端项目
前端·容器·kubernetes
鲸落落丶3 小时前
webpack学习
前端·学习·webpack
何中应3 小时前
MyBatis-Plus字段类型处理器使用
java·数据库·后端·mybatis
excel3 小时前
深入理解 3D 火焰着色器:从 Shadertoy 到 Three.js 的完整实现解析
前端