Delphi调用HTTP接口三种参数拼接方式

1,常规的+号连接运算符拼接

URL:=URL+'&eid='+FEID+'&templatecode='+TemplateCode+'&phone='+sPhone;
URL:=URL+'&signature='+signature+'&sign_name='+FSignName+'&timestamp='+sTimestamp+'&canshu='+sParam.AsString;
或者直接使用Format格式化拼接
URL:=Format('http://sms.xxx.com/index/xxx.php?request=%s&eid=%s&templatecode=%s&phone=%s&signature=%s&sign_name=%s&timestamp=%s&canshu=%s',
['public.xxx.action', FEID, TemplateCode,
sPhone, Signature, FSignName, sTimestamp, sParam.AsString]);

2,使用TIdMultiPartFormDataStream,需要引用IdMultipartFormData单元

Param:= TIdMultiPartFormDataStream.Create;
Param.AddFormField('request', 'public.xxxx.action');
Param.AddFormField('eid', FEID);
Param.AddFormField('templatecode', TemplateCode);
Param.AddFormField('phone', sPhone);
Param.AddFormField('signature', signature);
Param.AddFormField('sign_name', FSignName);
Param.AddFormField('timestamp', sTimestamp);
Param.AddFormField('canshu', sParam.AsString);
idHttp1.Post(URL, Param, ResponseStream);

3,使用TStringList拼接参数

ParamList := TStringList.Create;
ParamList.Add('request=public.xxx.action');
ParamList.Add('eid='+ FEID);
ParamList.Add('templatecode='+ TemplateCode);
ParamList.Add('phone='+ sPhone);
ParamList.Add('signature=' + Signature);
ParamList.Add('sign_name=' + FSignName);
ParamList.Add('timestamp=' + sTimestamp);
ParamList.Add('canshu=' + sParam.AsString);
idHTTP1.Post(URL, ParamList, ResponseStream);

经实际测试后两种方法经常会遇到不好使的情况。第一种笨方法最有效。

posted @   IT情深  阅读(249)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示