using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Net; using System.Xml; namespace ThreadTestSMGW { public class SMSHandler { public static string SITEURL = System.Configuration.ConfigurationManager.AppSettings["URL"]; public string errmsg = ""; /// /// 发送短信 /// /// /// /// /// /// /// public string SendSMS(string account, string password, string extendNo, string mobileNo, string content) { if ( string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(mobileNo) || string.IsNullOrEmpty(content)) { //log4netService.Info(userid+account+password+mobileNo+content); errmsg = "参数不全"; return errmsg; } /* string postData = string.Format("action=send&account=账号&password=密码&mobile=15023239810,13527576163&content=内容&extno=1069012345"); */ //string requestUrl = string.Format("http://8.129.227.30:7862/sms"); string requestUrl = string.Format(SITEURL+"/sms"); StreamReader reader; Uri requestUri = new Uri(requestUrl); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; request.Timeout = 1000000; StringBuilder data = new StringBuilder(); data.Append("action=send"); data.Append(string.Format("&account={0}", account)); data.Append(string.Format("&password={0}", password)); data.Append(string.Format("&extno={0}", extendNo)); data.Append(string.Format("&mobile={0}", mobileNo)); data.Append(string.Format("&content={0}", content+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))); //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString()); byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换 request.ContentLength = byteData.Length; // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } WebResponse response = (HttpWebResponse)request.GetResponse(); //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")); reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); StringBuilder builder = new StringBuilder(); char[] buffer = new char[0x400]; int charCount = 0; while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0) { builder.Append(buffer, 0, charCount); } reader.Close(); string result = builder.ToString(); if (string.IsNullOrEmpty(result)) { errmsg = "未知异常"; return errmsg; } XmlDocument docxml = new XmlDocument(); docxml.LoadXml(result); //DataSql ds = new DataSql(); string r = docxml.SelectSingleNode("returnsms/returnstatus").InnerText; if (r == "Success") { errmsg = docxml.SelectSingleNode("returnsms/taskID").InnerText; //ds.UpdateSql(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),id); return errmsg;// +","+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); } else { errmsg = docxml.SelectSingleNode("returnsms/message").InnerText; //ds.UpdateSql(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), id); return errmsg;// +"," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); } } /// /// 点对点短信发送 /// /// /// /// /// /// public string SendSMS(string account, string password, string extendNo, string mobileConentList) { if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(mobileConentList)) { //log4netService.Info(userid+account+password+mobileNo+content); errmsg = "参数不全"; return errmsg; } string requestUrl = string.Format(SITEURL + "/sms"); StreamReader reader; Uri requestUri = new Uri(requestUrl); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; request.Timeout = 1000000; StringBuilder data = new StringBuilder(); data.Append("action=p2p"); data.Append(string.Format("&account={0}", account)); data.Append(string.Format("&password={0}", password)); data.Append(string.Format("&extno={0}", extendNo)); data.Append(string.Format("&mobileContentList={0}", mobileConentList)); data.Append(string.Format("&rt=json")); //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString()); byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换 request.ContentLength = byteData.Length; // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } WebResponse response = (HttpWebResponse)request.GetResponse(); //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")); reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); StringBuilder builder = new StringBuilder(); char[] buffer = new char[0x400]; int charCount = 0; while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0) { builder.Append(buffer, 0, charCount); } reader.Close(); string result = builder.ToString(); if (string.IsNullOrEmpty(result)) { errmsg = "未知异常"; return errmsg; } return result; //XmlDocument docxml = new XmlDocument(); //docxml.LoadXml(result); //string r = docxml.SelectSingleNode("returnsms/returnstatus").InnerText; //if (r == "Success") //{ // errmsg = docxml.SelectSingleNode("returnsms/taskID").InnerText; // return errmsg; //} //else //{ // errmsg = docxml.SelectSingleNode("returnsms/message").InnerText; // return errmsg; //} } public string Report(string account, string password) { if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password)) { //log4netService.Info(userid+account+password+mobileNo+content); errmsg = "参数不全"; return errmsg; } /* string postData = string.Format("action=send&account=账号&password=密码&mobile=15023239810,13527576163&content=内容&extno=1069012345"); */ string requestUrl = string.Format(SITEURL); StreamReader reader; Uri requestUri = new Uri(requestUrl); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; request.Timeout = 1000000; StringBuilder data = new StringBuilder(); data.Append("action=report"); data.Append(string.Format("&account={0}", account)); data.Append(string.Format("&password={0}", password)); //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString()); byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换 request.ContentLength = byteData.Length; // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } WebResponse response = (HttpWebResponse)request.GetResponse(); //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")); reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); StringBuilder builder = new StringBuilder(); char[] buffer = new char[0x400]; int charCount = 0; while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0) { builder.Append(buffer, 0, charCount); } reader.Close(); string result = builder.ToString(); if (string.IsNullOrEmpty(result)) { errmsg = "未知异常"; return errmsg; } return "状态报告" + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); } } }