public Boolean DoSMS(InSMS inSMS) { Boolean result = true; ESMSLog smsLog = new ESMSLog(); try { SMSCore sms = new SMSCore(); SMSResult_API51 Response = sms.PostSMS_API51(inSMS, ref smsLog); if (Response.result == "0") { result = true; } else { result = false; } } catch (Exception ex) { smsLog.Exception += "DoSMS Error:" + ex.Message; smsLog.Exception += "DoSMS Inner Error:" + ex.InnerException.Message; result = false; } smsLog.IsSuccess = result; _dbContext.DBSMSLog.Add(smsLog); _dbContext.SaveChanges(); return(result); }
public OutSMS RequireVerifyCode(string mobilePhone, int IntervalSec, SMSEvent SMSEvent = SMSEvent.UserPhoneVerify) { OutSMS OutSMS = new OutSMS(); try { OutSMS = GetVerifyingSec(mobilePhone, IntervalSec); //说明可以重新发送短信(不在短信重新发送倒计时内) if (OutSMS.RemainSec == -1) { string VerifyCode = GetRnd(6, true, false, false, false, ""); InSMS inSMS = new InSMS(); inSMS.Init(); inSMS.Tpl_id = Convert.ToInt32(SMSTemplate.NormalVerify).ToString(); inSMS.PhoneNumber = mobilePhone; inSMS.Parameters = VerifyCode + "," + SMSMaxIntervalSec / 60; if (!this.DoSMS(inSMS)) { OutSMS.SMSVerifyStatus = SMSVerifyStatus.SentFailure; return(OutSMS); } ESMSVerification sms = new ESMSVerification() { VerifyCode = VerifyCode, MobilePhone = mobilePhone, SendDateTime = DateTime.Now, SMSVerifyStatus = SMSVerifyStatus.Sent, SMSEvent = SMSEvent, }; _dbContext.DBSMSVerification.Add(sms); _dbContext.SaveChanges(); OutSMS.SmsID = sms.ID; OutSMS.SMSVerifyStatus = SMSVerifyStatus.Sent; OutSMS.RemainSec = -1; } else { OutSMS.RemainSec = IntervalSec - OutSMS.RemainSec; OutSMS.SMSVerifyStatus = SMSVerifyStatus.Verifying; } } catch (Exception ex) { throw ex; } return(OutSMS); }
public SMSResult_API51 PostSMS_API51(InSMS InSMS, ref ESMSLog smsLog) { try { smsLog.APPName = "API51"; smsLog.UserPhone = InSMS.PhoneNumber; String querys = ""; String bodys = "mobile={0}¶ms={1}&sign={2}&tpl_id={3}"; String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; bodys = string.Format(bodys, InSMS.PhoneNumber, InSMS.Parameters, InSMS.Sign, InSMS.Tpl_id); smsLog.RequestMessage = bodys; if (0 < querys.Length) { url = url + "?" + querys; } if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); //根据API的要求,定义相对应的Content-Type httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); smsLog.ResponseMessage = json; smsLog.SendDateTime = DateTime.Now; JsonSerializer serializer = new JsonSerializer(); StringReader sr = new StringReader(json); SMSResult_API51 result = serializer.Deserialize <SMSResult_API51>(new JsonTextReader(sr)); return(result); } catch (Exception ex) { smsLog.Exception += "SMSManager Post:" + ex.Message; throw ex; } }