首页 > 代码库 > 中国网建的短信接口实现

中国网建的短信接口实现

实现相对简单,发送手机号和内容到特定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 =http://www.mamicode.com/ 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;        }