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;

相关推荐
Csvn2 小时前
WASM
前端
w-w0w-w2 小时前
SQL注入
网络·数据库·sql
tianyuanwo2 小时前
NetBox 数据库详解:当“关系型数据库”被装进一个网络运维的领域模型里
运维·数据库·netbox
一条泥憨鱼2 小时前
【计算机网络】短连接、长连接与连接池:从 TCP 握手到池化复用的完整链路
网络·数据库·网络协议·tcp/ip·计算机网络
IvorySQL3 小时前
深入 PostgreSQL WAL:从崩溃恢复到逻辑复制
数据库·postgresql
修己xj3 小时前
我用 AI 给自己写了一个 Markdown 编辑器,然后把它开源了
前端
FRing033 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
幸运小猿4 小时前
模拟慢上传-处理超时重试image 丢失线上问题
前端·javascript·html
幽灵诶4 小时前
Mysql(基于测试的学习)
数据库·mysql
砚底藏山河4 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融