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;

相关推荐
wtsolutions4 分钟前
Excel-to-JSON插件专业版功能详解:让Excel数据转换更灵活
json·excel·excel-to-json·wtsolutions·专业版
Rverdoser8 分钟前
代理服务器运行速度慢是什么原因
开发语言·前端·php
牛马baby12 分钟前
Java高频面试之并发编程-16
java·开发语言·面试
kerryYG20 分钟前
使用JMETER中的JSON提取器实现接口关联
jmeter·json
Blossom.11823 分钟前
探索边缘计算:赋能物联网的未来
开发语言·人工智能·深度学习·opencv·物联网·机器学习·边缘计算
C_Liu_39 分钟前
C语言:深入理解指针(3)
c语言·数据结构·算法
饕餮争锋41 分钟前
feign.RequestInterceptor 简介-笔记
java·开发语言·笔记
huangyuchi.43 分钟前
【C++】智能指针
开发语言·jvm·c++·笔记·c++11·智能指针·shared_ptr
南玖yy1 小时前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计
李匠20241 小时前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang