首先说明一下,这是我平生第一次接到 接口文档,认知有限,写得不对的地方,请大佬们多多指点.
下面是一个智能刀具柜关于库存查询的接口文档:
上面的参数中,appid 和 sign中的appsecret和macno是设备的常量值,要问设备服务商给.
上面的接口要分别设置header头和请求参数.而POST提交的地址是:请求地址(图1打码部分)+接口的请求路径(/api/external/record/getStock).我以TNetHTTPClient控件为例来配置这个接口.
第一部从服务商那里获得appid 和appsecret后,写出sign的计算函数(sign string 签名: md5(appid+timestamp+appsecret))
uses IdHashMessageDigest; function Calcsign(appid, timestamp, appsecret: string): string; // MD5加密 var md5: TIdHashMessageDigest5; begin md5 := TIdHashMessageDigest5.Create; try //这里一定要转成小写 Result := lowercase(md5.HashstringAsHex(appid + timestamp + appsecret)); finally md5.Free; end; end;
然后是配置接口
procedure TForm1.Button1Click(Sender: TObject); var HttpClient: TNetHTTPClient; Response: IHTTPResponse; appid, timestamp, sign, url: string; postData: TStringList; begin appid := '99999'; //服务商会提供 timestamp := IntToStr(DateTimeToUnix(Now, false)); sign := Calcsign(appid, timestamp, 'XXXXX'); //MD5加密.其中第三个参数服务商会提供 url := '接口的提交地址'; //这里的地址被我改掉了 HttpClient := TNetHTTPClient.Create(nil); // header参数设置 HttpClient.CustomHeaders['appid']:= appid; HttpClient.CustomHeaders['timestamp']:= timestamp; HttpClient.CustomHeaders['sign']:= sign; Httpclient.ContentType := 'application/x-www-form-urlencoded;'; // POST参数 postData := TStringList.Create; postData.AddPair('macno', 'xxxxx');//其中第二个参数服务商会提供
postData.AddPair('goods_code', '004.202'); //要查询库存的编码
{$IFDEF MSWINDOWS} // 检查操作系统版本为 Windows 7 (DELPHI WIn7下使用 NetHttpClient 请求HTPPS 网站 - 一曲轻扬 - 博客园 (cnblogs.com))
if (Win32MajorVersion = 6) and (Win32MinorVersion = 1) then
HttpClient.SecureProtocols := [THTTPSecureProtocol.TLS12];
{$ENDIF}
try
Response := HttpClient.Post(url, postData);
Memo1.Lines.Text := Response.ContentAsString; //把返回的数据体现出来,你也可以保存成文件.
//接下来你可能需要把数据洗一下才能使用,因为它返回的是json的格式
finally
HttpClient.Free;
postData.Free;
end;
end;
//返回的数据
{"code":1,"msg":"success","time":"1700637003","data":[{"machine_name":"xx智能刀具柜","machine_number":1,"data":[{"goods_code":"004.202","goods_name":"004.202\/涂层燕尾刀D1.0*A90*C5*D4*2F*50L\/支","stock_number":"0"}]}]}
标签:postData,timestamp,delphi,接触,接口,sign,appid,HttpClient From: https://www.cnblogs.com/yoooos/p/17849100.html