/// <summary> /// 发送短信 /// </summary> /// <param name="sms"></param> /// <returns></returns> public async Task<ResponseData<SendSmsResult>> SendSmsAsync(SendCloudSms sms) { if (sms == null) throw new ArgumentNullException(nameof(sms)); if (string.IsNullOrWhiteSpace(this.Configuration.SmsUser)) throw new ArgumentNullException(nameof(this.Configuration.SmsUser)); if (string.IsNullOrWhiteSpace(this.Configuration.SmsKey)) throw new ArgumentNullException(nameof(this.Configuration.SmsKey)); Contract.EndContractBlock(); sms.Validate(); var timestampResponse = await this.GetServerTimeStampAsync(); var timestamp = timestampResponse.Info.TimeStamp; var credential = new Credential(this.Configuration.SmsUser, this.Configuration.SmsKey); var treeMap = new SortedDictionary<string, string>(); treeMap.Add("smsUser", credential.ApiUser); treeMap.Add("msgType", sms.MsgType.ToString()); treeMap.Add("phone", sms.GetPhoneString()); treeMap.Add("templateId", sms.TemplateId.ToString()); treeMap.Add("timestamp", timestamp.ToString()); if (sms.Vars?.Count > 0) { treeMap.Add("vars", sms.GetVarsString()); } var signature = Md5Utils.MD5Signature(treeMap, credential.ApiKey); treeMap.Add("signature", signature); using (var client = new HttpClient()) { var response = await client.PostAsync(this.Configuration.SendSmsApi, new FormUrlEncodedContent(treeMap)); return await this.ValidateAsync<SendSmsResult>(response); } }
/// <summary> /// 发送语音 /// </summary> /// <param name="voice"></param> /// <returns></returns> public async Task<ResponseData<dynamic>> SendVoiceAsync(SendCloudVoice voice) { if (voice == null) throw new ArgumentNullException(nameof(voice)); if (string.IsNullOrWhiteSpace(this.Configuration.SmsUser)) throw new ArgumentNullException(nameof(this.Configuration.SmsUser)); if (string.IsNullOrWhiteSpace(this.Configuration.SmsKey)) throw new ArgumentNullException(nameof(this.Configuration.SmsKey)); Contract.EndContractBlock(); voice.Validate(); var timestampResponse = await this.GetServerTimeStampAsync(); var timestamp = timestampResponse.Info.TimeStamp; var credential = new Credential(this.Configuration.SmsUser, this.Configuration.SmsKey); var treeMap = new SortedDictionary<string, string>(); treeMap.Add("smsUser", credential.ApiUser); treeMap.Add("phone", voice.Phone); treeMap.Add("code", voice.Code); treeMap.Add("timestamp", timestamp.ToString()); var signature = Md5Utils.MD5Signature(treeMap, credential.ApiKey); treeMap.Add("signature", signature); using (var client = new HttpClient()) { var response = await client.PostAsync(this.Configuration.SendVoiceApi, new FormUrlEncodedContent(treeMap)); return await this.ValidateAsync(response); } }