asp.net C# 实现阿里大鱼和云片网短信接口类

2021-05-02 09:29

阅读:360

标签:手机   url   trim   string   pps   个数   use   asp.net   product   

云片网短信通用类

public class YunpianSMS
    {
        public YunpianSMS()
        { }

        /// 
        /// 服务器HTTP地址
        /// 
        private static string BASE_URI = "http://yunpian.com";

        /// 
        /// 服务版本号
        ///       
        private static string VERSION = "v1";

        /// 
        /// 查账户信息的http地址 
        ///            
        private static string URI_GET_USER_INFO = BASE_URI + "/" + VERSION + "/user/get.json";

        /// 
        /// 通用接口发短信的http地址 
        /// 
        private static string URI_SEND_SMS = BASE_URI + "/" + VERSION + "/sms/send.json";

        /// 
        /// 模板接口短信接口的http地址 
        /// 
        private static string URI_TPL_SEND_SMS = BASE_URI + "/" + VERSION + "/sms/tpl_send.json";

        /// 
        /// 通用接口查回复的短信的http地址 
        /// 
        private static string URI_GET_REPLY = BASE_URI + "/" + VERSION + "/sms/get_reply.json";

        /// 
        /// APIKEY
        /// 
        private static string APIKEY = "APIKEY";

        /// 
        /// 获取用户信息
        /// 
        /// Json格式
        public static string GetUserInfo()
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI_GET_USER_INFO + "?apikey=" + APIKEY);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }

        /// 
        ///  发短信通用接口
        /// 
        /// 短信内容
        /// 接收的手机号码,有多个手机号则用逗号分隔,一次最多100个手机号码
        /// Json格式 
        public static string SendSms(string text, string mobile)
        {
            //注意:参数必须进行Uri.EscapeDataString编码。以免%=等特殊符号无法正常提交
            string parameter = "apikey=" + APIKEY + "&mobile=" + mobile + "&text=" + text;
            return HttpPost(URI_SEND_SMS, parameter);
        }

        /// 
        /// 模板接口发短信
        /// 
        /// 模板ID
        /// 接收的手机号码
        /// 模板变量值
        /// Json格式
        public static string TplSendSms(long tpl_id, string mobile, string tpl_value)
        {
            string postDataStr = "apikey=" + APIKEY + "&mobile=" + mobile + "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
            return HttpPost(URI_TPL_SEND_SMS, postDataStr);
        }

        /// 
        /// 查回复的短信
        /// 
        /// 页码,从1开始
        /// 每页个数,最大100个
        /// 接收的手机号码
        /// Json格式
        public static string GetReplySms(int page_num, int page_size, string mobile,string datastart,string dataend)
        {
            DateTime now = DateTime.Now;
            //string datastart = now.AddDays(-3).ToString("yyyy-MM-dd 00:00:00");
            //string datasend = now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            string postDataStr = "apikey=" + APIKEY + "&start_time=" + datastart + "&end_time=" + dataend
                + "&page_num=" + page_num + "&page_size=" + page_size + "&mobile=" + mobile;

            return HttpPost(URI_GET_REPLY, postDataStr);
        }

        /// 
        /// 通用接口请求
        /// 
        /// 
        /// 
        /// 
        public static string HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                return res;
            }
            catch (Exception e)
            {
                return e.Message + e.ToString();
            }
        }
    }

调用短信模版方式:

string tpl_value = HttpUtility.UrlEncode(
                            HttpUtility.UrlEncode("#username#", Encoding.UTF8) + "=" +
                            HttpUtility.UrlEncode(phone, Encoding.UTF8) + "&" +
                            HttpUtility.UrlEncode("#passwd#", Encoding.UTF8) + "=" +
                            HttpUtility.UrlEncode(passwd, Encoding.UTF8), Encoding.UTF8);
                        //短信失败时,调用第二短信接口
                        YunpianSMS.TplSendSms(1508914, phone, tpl_value);

阿里大鱼短信通用类

public class AliDaYuSMS
    {
        /// 
        /// 
        /// Url
        /// 
        private static string Url = "http://gw.api.taobao.com/router/rest";
        /// AppKey
        /// 
        private static string AppKey = "AppKey";
        /// 
        /// AppSecret
        /// 
        private static string AppSecret = "AppSecret";

        /// 
        ///  发短信通用接口
        /// 
        /// 公共回传参数,
        /// 在“消息返回”中会透传回该参数;举例:用户可以传入自己下级的会员ID,在消息返回时,
        /// 该会员ID会包含在内,用户可以根据该会员ID识别是哪位会员使用了你的应用
        /// 短信签名
        /// 短信模板ID
        /// 短信模板变量“验证码${code},您正在进行${product}身份验证,打死不要告诉别人哦!”,
        /// 传参时需传入{"code":"1234","product":"alidayu"}
        /// 接收的手机号码,群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。
        /// Json格式 
        public static string SendSms(string extend, string smsFreeSignName, string code, string smsParam, string mobile)
        {
            ITopClient client = new DefaultTopClient(Url, AppKey, AppSecret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.Extend = extend;
            req.SmsType = "normal";
            req.SmsFreeSignName = smsFreeSignName;
            req.SmsParam = smsParam;
            req.RecNum = mobile;
            req.SmsTemplateCode = code;
            AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
            return rsp.SubErrMsg;
        }
    }

调用方式

                    var smsresult = AliDaYuSMS.SendSms(phone, "潮运动", "SMS_13000621", "{\"username\":\"" + phone + "\",\"passwd\":\"" + passwd + "\"}", phone);

 

asp.net C# 实现阿里大鱼和云片网短信接口类

标签:手机   url   trim   string   pps   个数   use   asp.net   product   

原文地址:http://www.cnblogs.com/yechangzhong-826217795/p/7767860.html


评论


亲,登录后才可以留言!