码迷,mamicode.com
首页 > 其他好文 > 详细

中国网建的短信接口实现

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

标签:style   blog   http   color   os   io   for   代码   

实现相对简单,发送手机号和内容到特定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

中国网建的短信接口实现

标签:style   blog   http   color   os   io   for   代码   

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

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