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

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

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;

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种编程语言,可以使用它来开发Windows平台的应用程序。如果你想在Delphi中发送POST请求并使用JSON数据,可以按照以下步骤操作: 1. 首先,你需要在Delphi中使用一个HTTP组件来发送HTTP请求。常见的组件包括Indy、Synapse和WinINet。 2. 在你的Delphi项目中导入所选HTTP组件的单元文件。 3. 创建一个HTTP客户端对象,并设置请求的URL和方法为POST。 4. 设置请求的Content-Type为application/json,以指示服务器你将发送JSON数据。 5. 创建一个JSON对象,并将要发送的数据填充到该对象中。 6. 将JSON对象转换为字符串,并将其作为请求的正文内容。 7. 发送请求并等待服务器的响应。 8. 处理服务器的响应,可以根据需要解析返回JSON数据。 下面是一个示例代码,使用Indy组件来发送POST请求并使用JSON数据: ```delphi uses IdHTTP, System.JSON; procedure SendJSONPostRequest; var HttpClient: TIdHTTP; RequestContent: TStringStream; ResponseContent: string; JsonRequest: TJSONObject; begin HttpClient := TIdHTTP.Create(nil); try JsonRequest := TJSONObject.Create; try // 构建JSON数据 JsonRequest.AddPair('name', 'John'); JsonRequest.AddPair('age', '30'); // 转换JSON为字符串 RequestContent := TStringStream.Create(JsonRequest.ToString, TEncoding.UTF8); try // 发送POST请求 HttpClient.Request.ContentType := 'application/json'; ResponseContent := HttpClient.Post('http://example.com/api', RequestContent); // 处理服务器响应 // ... finally RequestContent.Free; end; finally JsonRequest.Free; end; finally HttpClient.Free; end; end; ``` 以上代码中的URL为示例,请将其替换为实际的API地址。同时,请根据你实际使用的HTTP组件来修改代码。 希望能对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值