之前接客服电话让我这边提交问题,我们之前是用的以下代码(DELPHI XE10.1)发送短信的,现在阿里大鱼注册后不知道要怎么改,能否给一个DELPHI的DEMO?
或者能帮忙看下下面代码该怎么改吗?
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Layouts,
IPPeerClient, REST.Client, Data.Bind.Components, Data.Bind.ObjectScope,
System.JSON, // JSON库
REST.Types, FMX.Edit; // REST 常量库
type
TfrmMain = class(TForm)
GridPanelLayout1: TGridPanelLayout;
Memo1: TMemo;
Panel1: TPanel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Button1: TButton;
Image1: TImage;
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
edtUserID: TEdit;
Label2: TLabel;
edtToken: TEdit;
Label3: TLabel;
edtAppID: TEdit;
Label4: TLabel;
Label5: TLabel;
edtURL: TEdit;
edtVersion: TEdit;
Label6: TLabel;
Label1: TLabel;
edtMobile: TEdit;
procedure d(msg: string);
procedure setTimeStamp;
function getSigParameter: string;
function getAuthorizationHeader: string;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure initREST(URL: string; sJSONObject: TJSONObject;
restMethod: TRESTRequestMethod);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
globalTimeStamp: string;
mainAccountSID, mainAuthToken, mainAppID, mainRESTURL,
mainRESTVersion: string;
implementation
{$R *.fmx}
{$R *.iPhone55in.fmx IOS}
uses
Soap.EncdDecd, // BASE64编码
System.Hash; // MD5加密
procedure TfrmMain.Button1Click(Sender: TObject);
var
dJSONObject: TJSONObject;
dJSONArray: TJSONArray;
myBusinessURL: string; // 定义业务URL
begin
setTimeStamp; // 每次执行前,重置时间戳。
mainAccountSID := edtUserID.text;
mainAuthToken := edtToken.text;
mainAppID := edtAppID.text;
mainRESTURL := edtURL.text;
mainRESTVersion := edtVersion.text;
if RadioButton1.IsChecked then
begin
// 设置业务URL
// URL格式:/2013-12-26/Accounts/{accountSid}/SMS/TemplateSMS?sig={SigParameter}
// 当然也可以直接设置RESTRequest的Resource,但我喜欢直接写字符串。
myBusinessURL := mainRESTURL + '/' + mainRESTVersion + '/Accounts/' +
mainAccountSID + '/SMS/TemplateSMS?sig=' + getSigParameter;
dJSONObject := TJSONObject.Create;
dJSONObject.AddPair('to', edtMobile.text);
dJSONObject.AddPair('appId', mainAppID);
dJSONObject.AddPair('templateId', '1');
dJSONArray := TJSONArray.Create;
dJSONArray.Add('9999'); // 短信验证码 替换模板中的
dJSONArray.Add('10'); // 有效期 替换模板中的
dJSONObject.AddPair('datas', dJSONArray);
// d(dJSONObject.ToString); //查看一下发送内容;
initREST(myBusinessURL, dJSONObject, TRESTRequestMethod.rmPOST);
RESTRequest1.Execute;
end
else if RadioButton2.IsChecked then
begin
myBusinessURL := mainRESTURL + '/' + mainRESTVersion + '/Accounts/' +
mainAccountSID + '/AccountInfo?sig=' + getSigParameter;
dJSONObject := nil;
dJSONArray := nil;
initREST(myBusinessURL, nil, TRESTRequestMethod.rmGET);
RESTRequest1.Execute;
end;
d(RESTResponse1.Content);
end;
procedure TfrmMain.d(msg: string);
begin
Memo1.lines.Add(msg);
end;
procedure TfrmMain.initREST(URL: string; sJSONObject: TJSONObject;
restMethod: TRESTRequestMethod);
begin
RESTClient1.BaseURL := URL;
RESTClient1.Authenticator := nil; // 取消所有验证;
RESTClient1.Params.Clear; // 清除之前设置的各种参数;
RESTRequest1.Params.Clear;
RESTRequest1.ClearBody; // 清除之前设置的Body json;
RESTClient1.AddParameter('Authorization', getAuthorizationHeader,
TRESTRequestParameterKind.pkHTTPHEADER,
[TRESTRequestParameterOption.poDoNotEncode,
TRESTRequestParameterOption.poTransient]);
RESTClient1.AddParameter('Accept', 'application/json;', // 注意后面的分号,去掉是不行的。
TRESTRequestParameterKind.pkHTTPHEADER,
[TRESTRequestParameterOption.poDoNotEncode,
TRESTRequestParameterOption.poTransient]);
RESTClient1.AddParameter('Content-Type', 'application/json;charset=utf-8;',
// 注意后面的分号,去掉是不行的。
TRESTRequestParameterKind.pkHTTPHEADER,
[TRESTRequestParameterOption.poDoNotEncode,
TRESTRequestParameterOption.poTransient]);
if sJSONObject <> nil then
begin
RESTClient1.AddParameter('Content-Length',
IntToStr(Length(sJSONObject.ToString)),
TRESTRequestParameterKind.pkHTTPHEADER,
[TRESTRequestParameterOption.poDoNotEncode,
TRESTRequestParameterOption.poTransient]);
RESTRequest1.AddBody(sJSONObject);
end;
RESTRequest1.Method := restMethod;
end;
procedure TfrmMain.setTimeStamp;
begin
// 预先设定时间戳,而不是在需要的时候去取,避免在多处使用时出现时间不同导致验证失败。
globalTimeStamp := FormatDateTime('yyyymmddhhnnss', now);
end;
function TfrmMain.getSigParameter: string;
var
MD5: THashMD5;
begin
// 获取sig校验字符串
if globalTimeStamp = '' then
setTimeStamp;
Result := UpperCase(MD5.GetHashString(mainAccountSID + mainAuthToken +
globalTimeStamp));
end;
procedure TfrmMain.FormActivate(Sender: TObject);
begin
edtURL.text := 'https://sandboxapp.cloopen.com:8883';
edtVersion.text := '2013-12-26';
edtUserID.Text := ''; //在这里填上自己的测试账号即可运行。
edtToken.Text := '';
edtAppID.Text := '';
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
globalTimeStamp := '';
end;
function TfrmMain.getAuthorizationHeader: string;
begin
// 获取头验证信息字符串。
if globalTimeStamp = '' then
setTimeStamp;
Result := EncodeString(mainAccountSID + ':' + globalTimeStamp);
end;
// function sendTemplateSMS($to,$datas,$tempId)
end.