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;

相关推荐
ㄟ留恋さ寂寞3 分钟前
Golang怎么限制请求Body大小_Golang如何防止客户端发送过大的请求体【避坑】
jvm·数据库·python
之歆11 分钟前
DAY_11JavaScript BOM与DOM深度解析:底层原理与工程实践(上)
开发语言·前端·javascript·ecmascript
老纪13 分钟前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
会编程的土豆15 分钟前
Go ini 配置加载:`ini.MapTo` 详细解析
开发语言·数据库·golang
冴羽yayujs15 分钟前
GitHub 前端热榜项目 - 日榜(2026-05-17)
前端·github
老马952715 分钟前
opencode8-桌面应用实战 3
前端·人工智能·后端
逆yan_18 分钟前
🧭 基于 pnpm Workspace 和 Turborepo 的 Monorepo 最佳实践
前端·javascript·架构
今天也是元气满满的一天呢23 分钟前
详解SQL注入问题
网络·数据库·sql
omenkk725 分钟前
【MySQL专题】1.一条更新SQL语句是如何执行的
数据库·sql·mysql
2301_8092445328 分钟前
mysql如何处理大量重复值索引_mysql索引存储特征分析.txt
jvm·数据库·python