delphi idhttp post json 上传 PHP $_post 接收

前提:PHP端用$_post 接收

一、delphi idhttp post 上传数据 时如果使用 temp_stream: TStringStream;  内存流的方式上传 则需要指明IDHTTP的contentype

var
  idhttp2: TIdHTTP;
  temp_stream: TStringStream;
  temp_url: string;
  temp_str: string;
  strlist_para:TStringList;
begin
  strlist_para:=TStringList.Create();
  idhttp2 := TIdHTTP.Create(nil);
  strlist_para.Add('xx={"i":"5","billids":[{"id":1},{"id":2},{"id":3}]}') ;
  idhttp2.ReadTimeout := 10000; // 请求超时设置
  idhttp2.Request.ContentType := 'application/x-www-form-urlencoded';
  temp_stream := TStringStream.Create(AnsiToUtf8('xx='+json.AsString)); // 创建一个包含JSON数据的变量
  temp_stream.Position := 0; // 将流位置置为0
  temp_stream.SaveToFile('c:\123.txt');
  temp_stream.Position := 0; // 将流位置置为0
  temp_url := '地址';
  try
    temp_str := idhttp2.Post(temp_url, temp_stream);
  except
    temp_str := '';
  end;
  idhttp2.Free();
  temp_stream.Free;
  result := temp_str;

二、strlist_para:TStringList;方式上传

var
  idhttp2: TIdHTTP;
  temp_stream: TStringStream;
  temp_url: string;
  temp_str: string;
  strlist_para:TStringList;
begin
  strlist_para:=TStringList.Create();
  idhttp2 := TIdHTTP.Create(nil);
  strlist_para.Add('xx={"i":"5","billids":[{"id":1},{"id":2},{"id":3}]}') ;
  idhttp2.ReadTimeout := 10000; // 请求超时设置
  idhttp2.Request.ContentType := 'application/x-www-form-urlencoded';
  temp_stream := TStringStream.Create(AnsiToUtf8('xx='+json.AsString)); // 创建一个包含JSON数据的变量
  temp_stream.Position := 0; // 将流位置置为0
  temp_stream.SaveToFile('c:\123.txt');
  temp_stream.Position := 0; // 将流位置置为0
  temp_url := '地址';
  try
    temp_str := idhttp2.Post(temp_url, strlist_para);
  except
    temp_str := '';
  end;
  idhttp2.Free();
  temp_stream.Free;
  result := temp_str;

 

Delphi 2010 是一个集成开发环境(IDE),供开发者使用 Object Pascal 编程语言。要使用 Delphi 2010 的 IdHTTP 组件进行 JSON 提交,首先需要将 IdHTTP 组件放置在窗体的设计区域或者手动创建一个 IdHTTP 对象。 在代码中,需要导入 IdHTTP 单元,然后创建一个 IdHTTP 对象。然后,使用 Post 方法将 JSON 数据提交到服务器。在 Post 方法中,我们需要设置请求的 URL,并将 JSON 数据转换为字节数组。 具体代码如下: 1. 首先,确保你已经导入 IdHTTP 单元: ``` uses IdHTTP; ``` 2. 在窗体或类的声明部分,添加一个私有变量来持有 IdHTTP 对象: ``` private Http: TIdHTTP; ``` 3. 在创建窗体或类的构造函数中,创建 IdHTTP 对象并设置一些属性: ``` constructor TForm1.Create(AOwner: TComponent); begin inherited Create(AOwner); Http := TIdHTTP.Create(Self); Http.HandleRedirects := True; end; ``` 4. 在需要提交 JSON 数据的地方,使用以下代码: ``` procedure TForm1.PostJSONData(const URL: string; const JSONData: string); var Request: TStringStream; Response: string; begin Request := TStringStream.Create(JSONData, TEncoding.UTF8); try Response := Http.Post(URL, Request); ShowMessage(Response); // 可以根据需要处理服务器的响应结果 finally Request.Free; end; end; ``` 在这个例子中,URL 是目标服务器的地址,JSONData 是要提交JSON 数据。完成后,你可以根据需要处理服务器的响应结果。 这是使用 Delphi 2010 的 IdHTTP 组件提交 JSON 数据的简单示例。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值