标签: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; }
标签:style blog http color os io for 代码
原文地址:http://www.cnblogs.com/ysf123/p/3872695.html