首页 > 其他 > 详细

中国网建的短信接口实现

时间:2014-07-28 14:52:13      阅读:379      评论:0      收藏:0      [点我收藏+]

实现相对简单,发送手机号和内容到特定url就行了,代码如下:

public string SendSMS(string mobile, string content)
        {
            string UserName = "账号";//网建账号
            string PassWord = "密码";//提供的密码
            string url = "http://utf8.sms.webchinese.cn";//路径
            string result = "";
            int re = 0;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded";
                StringBuilder buffer = new StringBuilder();
                buffer.AppendFormat("Uid={0}", UserName);
                buffer.AppendFormat("&Key={0}", PassWord);
                buffer.AppendFormat("&smsMob={0}", mobile);
                buffer.AppendFormat("&smsText={0}", HttpUtility.UrlEncode(content + "【内部管理系统】", Encoding.UTF8));
                byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string msg = sr.ReadToEnd();
                sr.Close();
                int.TryParse(msg, out re);
                if(re<=0)
                {
                    result = "短信发送失败!错误代码:" + msg;
                }
                else
                {
                    result = "短信发送成功";
                }
            }
            catch (Exception ex)
            {
                result = "短信发送失败!错误信息:" + ex;
            }
            return result;
        }

 

中国网建的短信接口实现,布布扣,bubuko.com

中国网建的短信接口实现

原文:http://www.cnblogs.com/ysf123/p/3872695.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!