码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-11-01 18:56:45      阅读:224      评论:0      收藏:0      [点我收藏+]

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

云片网短信通用类

public class YunpianSMS
    {
        public YunpianSMS()
        { }

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

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

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

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

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

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

        /// <summary>
        /// APIKEY
        /// </summary>
        private static string APIKEY = "APIKEY";

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns>Json格式</returns>
        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();
        }

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

        /// <summary>
        /// 模板接口发短信
        /// </summary>
        /// <param name="tpl_id">模板ID</param>
        /// <param name="mobile">接收的手机号码</param>
        /// <param name="tpl_value">模板变量值</param>
        /// <returns>Json格式</returns>
        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);
        }

        /// <summary>
        /// 查回复的短信
        /// </summary>
        /// <param name="page_num">页码,从1开始</param>
        /// <param name="page_size">每页个数,最大100个</param>
        /// <param name="mobile">接收的手机号码</param>
        /// <returns>Json格式</returns>
        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);
        }

        /// <summary>
        /// 通用接口请求
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        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
    {
        /// <summary>
        /// <summary>
        /// Url
        /// </summary>
        private static string Url = "http://gw.api.taobao.com/router/rest";
        /// AppKey
        /// </summary>
        private static string AppKey = "AppKey";
        /// <summary>
        /// AppSecret
        /// </summary>
        private static string AppSecret = "AppSecret";

        /// <summary>
        ///  发短信通用接口
        /// </summary>
        /// <param name="extend">公共回传参数,
        /// 在“消息返回”中会透传回该参数;举例:用户可以传入自己下级的会员ID,在消息返回时,
        /// 该会员ID会包含在内,用户可以根据该会员ID识别是哪位会员使用了你的应用</param>
        /// <param name="smsFreeSignName">短信签名</param>
        /// <param name="code">短信模板ID</param>
        /// <param name="smsParam">短信模板变量“验证码${code},您正在进行${product}身份验证,打死不要告诉别人哦!”,
        /// 传参时需传入{"code":"1234","product":"alidayu"}</param>
        /// <param name="mobile">接收的手机号码,群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。</param>
        /// <returns>Json格式</returns> 
        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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!