delphi 12 idhttpsever(S)+idhttp(C) 实现简单的JSON API服务

这篇博客展示了如何使用Delphi创建一个简单的HTTP服务器,并处理GET和POST请求。服务器监听6600端口,响应JSON格式的数据。客户端通过IdHttp组件进行GET和POST请求,获取并显示服务器响应的内容。

http服务器测试代码

procedure TForm1.FormShow(Sender: TObject);

begin

IdHTTPServer1.Bindings.Clear;

IdHTTPServer1.DefaultPort:= 6600;

IdHTTPServer1.Bindings.Add.IP := '127.0.0.1';

//启动服务器

IdHTTPServer1.Active := True;

end;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;

ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);

var

I: Integer;

begin

if SameText(ARequestInfo.Command, 'get') then

begin

if ARequestInfo.Document = '/api_v1/get_token' then

begin

Memo1.Lines.Add('-------------');

Memo1.Lines.Add(ARequestInfo.Params.Count.ToString);

Memo1.Lines.Add('-------------');

for I := 0 to ARequestInfo.Params.Count - 1 do

begin

Memo1.Lines.Add(ARequestInfo.Params.ValueFromIndex[I]);

end;

Memo1.Lines.Add('-------------');

AResponseInfo.CharSet := 'UTF-8';

AResponseInfo.ContentType := 'application/json';

AResponseInfo.ContentText := '{a:"001", b:"002", c:[ a:"003", b:"004"]}';

end;

end;

if SameText(ARequestInfo.Command, 'post') then

begin

if ARequestInfo.Document = '/api_v2/get_token' then

begin

Memo1.Lines.Add('-------------');

Memo1.Lines.Add(ARequestInfo.Params.Count.ToString);

Memo1.Lines.Add('-------------');

for I := 0 to ARequestInfo.Params.Count - 1 do

begin

Memo1.Lines.Add(ARequestInfo.Params.ValueFromIndex[I]);

end;

Memo1.Lines.Add('-------------');

AResponseInfo.CharSet := 'UTF-8';

AResponseInfo.ContentType := 'application/json';

AResponseInfo.ContentText := '{a:"0011", b:"0022", c:[ a:"0033", b:"0044"]}';

end;

end;

end;

客户端DEMO

客户端DEMO

procedure TForm2.Button1Click(Sender: TObject);

var

ttt: String;

begin

ttt := IdHttp1.Get('http://127.0.0.1:6600/api_v1/get_token?a=1\&b=2');

memo1.Text := ttt;

end;

procedure TForm2.Button2Click(Sender: TObject);

var

Sendmessage:TStringList;//发送内容

Receivemessage:TStringStream;//返回内容

ttt: String;

begin

Sendmessage:=TStringList.Create;

Receivemessage:=TStringStream.Create('');

Sendmessage.Add('ID=1001');//必须要有Add('字段=值')这种模式,否则传递过去服务端接收的是空值

Sendmessage.Add('name=jack');//还可以用Param.Add(head+middle+Edit1.text)的方式连接成有效的数组

Sendmessage.Add('sex=male');

IdHTTP1.ReadTimeout:=10000;//设置十秒后超时

IdHttp1.Post('http://127.0.0.1:6600/api_v2/get_token',Sendmessage, Receivemessage);

memo1.Text:=Receivemessage.DataString;//显示返回的值

Sendmessage.Free;

Receivemessage.Free;

end;

相关推荐
wjs20244 分钟前
HTML 字符实体
开发语言
二十雨辰12 分钟前
[Java基础]网络编程
java·开发语言
AC使者26 分钟前
介绍 TensorFlow 的基本概念和使用场景。
开发语言·自然语言处理·sqlite·github
kiramario1 小时前
【结束】JS如何不通过input的onInputFileChange使用本地mp4文件并播放,nextjs下放入public文件的视频用video标签无法打开
开发语言·javascript·音视频
土豆儿@2 小时前
java之泛型
java·开发语言
m0_748245342 小时前
python——Django 框架
开发语言·python·django
曼巴UE52 小时前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
熬夜苦读学习2 小时前
Linux文件系统
linux·运维·服务器·开发语言·后端
菜鸟一枚在这2 小时前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
阿巴~阿巴~3 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先