using System; using System.IO; using System.Net; using System.Text; using WX.CRM.CRMServices.SMS.InterfaceModel; namespace WX.CRM.CRMServices.SMS.InterfaceExec { public class HuiZhi : IMsgSend { string _Assembly = ""; string _Url = ""; string _UserName = ""; string _Pwd = ""; string _Rstype = "xml"; int _Pfex = 0; public HuiZhi(string account = "", string password = "") { HuiZhiConfig config = new HuiZhiConfig(); _Assembly = config.Assembly; _Url = config.Url; _UserName = config.UserName; _Pwd = config.Pwd; _Rstype = config.Rstype; _Pfex = config.Pfex; if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(password)) { _UserName = account; _Pwd = password; } } public InterfaceModel.ReturnResult Execute(string[] mobiles, string msg) { return Send(mobiles, msg); } public decimal queryAmt() { throw new NotImplementedException(); } #region 发送短信 InterfaceModel.ReturnResult Send(string[] mobiles, string msg) { InterfaceModel.ReturnResult result = new InterfaceModel.ReturnResult(); string Url = _Url; try { Encoding myEncoding = Encoding.GetEncoding("GBK"); String sResult = ""; string param = string.Format("command={0}&username={1}&pwd={2}&mobiles={3}&content={4}&incode=GBK&outcode=GBK&rstype={5}" , "sendSMSMD5" , _UserName , WX.CRM.Common.Utility.EncryptMD5(_Pwd).ToUpper() , string.Join(",", mobiles) , System.Web.HttpUtility.UrlEncode(msg, myEncoding) , _Rstype); result.SendStr = param; byte[] postBytes = Encoding.ASCII.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_Url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded;charset=GBK"; req.ContentLength = postBytes.Length; Stream webStream = req.GetRequestStream(); webStream.Write(postBytes, 0, postBytes.Length); webStream.Close(); HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(webResponse.GetResponseStream(), myEncoding); sResult = reader.ReadToEnd(); reader.Close(); sResult.Trim(); result.ReturnStr = sResult; sms info = WX.CRM.Common.Utility.XMLToObject(result.ReturnStr); if (info != null) result.submitStatus = info.result == "0" ? SMS.InterfaceModel.SubmitStatus.success : SMS.InterfaceModel.SubmitStatus.fail; else throw new Exception("反序列化异常,返回值:" + result.ReturnStr); } catch (Exception ex) { result.submitStatus = SMS.InterfaceModel.SubmitStatus.fail; WX.CRM.Common.LogHelper.Error("ExecHuiZhi.sendPkg:" + ex.Message); } return result; } public class sms { public string result { get; set; } public string prohibiteword { get; set; } public string sequence { get; set; } } #endregion } }