php 接收delphi post,Delphi 转载:Delphi 如何GET/POST 调用HTTP请求

Delphi 如何GET/POST 调用HTTP请求

https://blog.csdn.net/quan278905570/article/details/79724022

--------------------------**HTTP请求的GET方法**----------------------------------------

a42b3556487ae493ea53fe1bde29fbad4fc.jpg

07c107c6fcf4407f8d92f85f0874962483b.jpg

1 **HTTP请求的GET方法**

2 procedureGetDemo;3 var

4 IdHttp : TIdHTTP;5 Url : string;//请求地址

6 ResponseStream : TStringStream; //返回信息

7 ResponseStr : string;8 begin

9 //创建IDHTTP控件

10 IdHttp := TIdHTTP.Create(nil);11 //TStringStream对象用于保存响应信息

12 ResponseStream := TStringStream.Create('');13 try

14 //请求地址

15 Url := 'http://dict.youdao.com/';16 try

17 IdHttp.Get(Url,ResponseStream);18 except

19 on e : Exception do

20 begin

21 ShowMessage(e.Message);22 end;23 end;24 //获取网页返回的信息

25 ResponseStr :=ResponseStream.DataString;26 //网页中的存在中文时,需要进行UTF8解码

27 ResponseStr :=UTF8Decode(ResponseStr);28 finally

29 IdHttp.Free;30 ResponseStream.Free;31 end;32 end;

View Code

如果Get需要添加请求参数,则直接在地址后添加,各参数间用&连接

如:http://dict.youdao.com?param1=1&param2=2

---------------------------<><><><><><><><><><><>-----------------------------------------

---------------------------HTTP请求的GET方法----------------------------------------------

09aa0d01bfa83ca600aacf371e9574206d9.jpg

d15a41a25bfb0dd1fe6a07d45348bf47aa4.jpg

procedurePostDemo;varIdHttp : TIdHTTP;

Url :string;//请求地址

ResponseStream : TStringStream; //返回信息

ResponseStr : string;

RequestList : TStringList;//请求信息

RequestStream : TStringStream;begin

//创建IDHTTP控件

IdHttp := TIdHTTP.Create(nil);//TStringStream对象用于保存响应信息

ResponseStream := TStringStream.Create('');

RequestStream := TStringStream.Create('');

RequestList := TStringList.Create;tryUrl := 'http://f.youdao.com/?path=fanyi&vendor=fanyiinput';try

//以列表的方式提交参数

RequestList.Add('text=love');

IdHttp.Post(Url,RequestList,ResponseStream);//以流的方式提交参数

RequestStream.WriteString('text=love');

IdHttp.Post(Url,RequestStream,ResponseStream);excepton e : Exceptiondo

beginShowMessage(e.Message);end;end;//获取网页返回的信息

ResponseStr :=ResponseStream.DataString;//网页中的存在中文时,需要进行UTF8解码

ResponseStr :=UTF8Decode(ResponseStr);finallyIdHttp.Free;

RequestList.Free;

RequestStream.Free;

ResponseStream.Free;end;end;

View Code

Post请求在网页中多使用List形式提交参数。

不过在一些API中规定了POST的请求格式为 JSON 格式或 XML,这是需要注意发起请求前需要先设置 ContentType 属性,使用Stream方式提交

已上面代码为例:

提交 JSON 格式:IdHttp.Request.ContentType :=’application/json’;

提交 XML 格式: IdHttp.Request.ContentType :=’text/xml’;

如未按要求格式提交,一般会返回 HTTP 1.1 / 415

---------------------------<><><><><><><><><><><>-----------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值