美文网首页
c# 对接短信接口

c# 对接短信接口

作者: 张天择 | 来源:发表于2019-08-06 14:15 被阅读0次

private void GetPhoneCode(HttpContext context)

        {

            string mobile = GameRequest.GetQueryString("Mobile");//手机号

            AjaxJsonValid ajv = new AjaxJsonValid();

            ajv.SetValidDataValue(false);

            //手机号码校验

            if (string.IsNullOrWhiteSpace(mobile))

            {

                ajv.msg = "手机号码不能为空!";

                context.Response.Write(ajv.SerializeToJson());

                return;

            }

            //读取配置  webConfig中读取

            string smWebUrl = ApplicationSettings.Get("phonePostUrl").ToString();//接口地址

            string account = ApplicationSettings.Get("account").ToString();//商户账号

            string passworld = ApplicationSettings.Get("passworld").ToString();//商户密码

            passworld = Utility.MD5(passworld).ToUpper();  //转换为MD5大写

            string content = ApplicationSettings.Get("phoneContent").ToString();  //短信内容

            DateTime sendTime = DateTime.Now;    

            string action = "send";

            //生成验证码

            Random random = new Random();

            string code = string.Concat(new object[]

            {

              random.Next(1, 10),

              random.Next(1, 10),

              random.Next(1, 10),

              random.Next(1, 10)

            });

            //拼接数据

            string strContent = string.Format(content, code);

            string strSendFormat = "userid={0}&account={1}&password={2}&mobile={3}&content={4}&action={5}";

            string strSendContent = string.Format(strSendFormat, new object[] { "", account, passworld, mobile, strContent, action });

            //http请求

            string strBackContent = PushToWeb(smWebUrl, strSendContent, Encoding.UTF8);

            int pos1 = strBackContent.IndexOf("<returnstatus>");

            int pos2 = strBackContent.IndexOf("</returnstatus>");

            string sendStatus = strBackContent.Substring(pos1 + 14, pos2 - pos1 - 14);

            if (sendStatus != "Success")

            {

                int pos3 = strBackContent.IndexOf("</message>");

                int pos4 = strBackContent.IndexOf("<message>");

                string errorStr = strBackContent.Substring(pos4 + 9, pos3 - pos4 - 9);

                ajv.msg = errorStr;

                context.Response.Write(ajv.SerializeToJson());

            }

            else

            {

                int vercode = Convert.ToInt32(code);

                MessageVerCode messageVerCode = new MessageVerCode();

                messageVerCode.Mobile = mobile;

                messageVerCode.VerCode = vercode;

                //将短信数据存入数据库,方便客户端校验

                if (FacadeManage.aideAccountsFacade.AddMessage(messageVerCode) > 0)

                {

                    ajv.msg = "发送成功";

                    context.Response.Write(ajv.SerializeToJson());

                }

            }

        }


      //发送短信

        public string PushToWeb(string weburl, string data, Encoding encode)

        {

            byte[] bytes = encode.GetBytes(data);

            HttpWebRequest length = (HttpWebRequest)WebRequest.Create(new Uri(weburl));

            length.Method = "POST";

            length.ContentType = "application/x-www-form-urlencoded";

            length.ContentLength = (long)((int)bytes.Length);

            Stream requestStream = length.GetRequestStream();

            requestStream.Write(bytes, 0, (int)bytes.Length);

            requestStream.Close();

            return (new StreamReader(((HttpWebResponse)length.GetResponse()).GetResponseStream(), encode)).ReadToEnd();

        }

相关文章

网友评论

      本文标题:c# 对接短信接口

      本文链接:https://www.haomeiwen.com/subject/lowidctx.html