delphi实现post请求

delphi实现post请求,前提头部先引用IDHttp组件
ContentType := ‘application/x-www-form-urlencoded’;

var
fhttpRequest: TIdHTTP;
PostData : TStringStream;
tmpStr : String;
 
begin
 
fhttpRequest := TIdHTTP.Create(nil);
  fhttpRequest.HandleRedirects := True;//允许头转向
  fhttpRequest.ReadTimeout := 5000;//请求超时设置
  fhttpRequest.Request.ContentType := 'application/x-www-form-urlencoded';//设置内容类型为json
  PostData := TStringStream.Create('');
  //PostData.Position := 0;//将流位置置为0
  PostData.WriteString('encode=773130f7236b15405c9497c19bf24a038ba04197f017bfb0dd66d455bcb75e9e580');
  PostData.WriteString('302a5d177cae445ae6c97be24a74344c2cade3dee0015cda9e3ef05a71a9b32409f7d026e633a8125d8d62298aa1a5d35c50e40dc316a07b355d4f70b712b73f37befe911824a78ec556556951d2bf76ec4944e73cbc3b2b88ada31a85b09');
  tmpStr := fhttpRequest.Post('http://10.32.8.34:8081/phis/EncodeServlet',PostData); //tmpStr是提交后返回的数据
  fhttpRequest.free;
  PostData.Free;
  Result:=tmpStr;
end;

ContentType := ‘application/json’;

Delphi source:

http := TIdHttp.Create(nil);
http.HandleRedirects := True;//允许头转向
http.ReadTimeout := 5000;//请求超时设置
http.Request.ContentType := 'application/json';//设置内容类型为json
//http.Request.CustomHeaders.Values['Authorization'] := Concat('Bearer ',这里写token信息 );
jsonToSend := TStringStream.Create('{"name":"Peter Pan"}');//创建一个包含JSON数据的变量
jsonToSend.Position := 0;//将流位置置为0
Memo1.Lines.Text := http.Post('http://www.website.com/test.php', jsonToSend);//用MEMO控件接收POST后的数据返回
jsonToSend.free; http.free;//用完记得释放
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香煎三文鱼

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值