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);

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

猜你喜欢

转载自blog.csdn.net/wh445306/article/details/114954482