unigui用TNetHTTPClient提交post内容,并接收返回json数据例子
时间: 2023-02-09 19:19:10 浏览: 311
HttpClient发送post请求传输json数据
您好,这是一个使用 TNetHTTPClient 在 UniGUI 中提交 POST 请求并接收 JSON 数据的例子:
```
uses System.Net.HttpClient, System.JSON;
procedure TForm1.Button1Click(Sender: TObject);
var
HTTP: TNetHTTPClient;
Request: TNetHTTPRequest;
Response: TNetHTTPResponse;
Data: TJSONObject;
begin
HTTP := TNetHTTPClient.Create(nil);
try
Request := TNetHTTPRequest.Create(HTTP);
Request.Method := THTTPRequestMethod.POST;
Request.Resource := '/api/submit';
Request.ContentType := 'application/json';
Data := TJSONObject.Create;
Data.AddPair('field1', 'value1');
Data.AddPair('field2', 'value2');
Request.Content := Data.ToString;
Response := HTTP.Execute(Request);
try
if Response.StatusCode = 200 then
begin
Data := TJSONObject.ParseJSONValue(Response.ContentAsString) as TJSONObject;
// 处理返回的 JSON 数据
end
else
begin
// 处理错误
end;
finally
Response.Free;
end;
finally
HTTP.Free;
end;
end;
```
希望这能帮到您!
阅读全文