using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Net; using System.Text; using WX.CRM.CRMServices.SMS.InterfaceModel; namespace WX.CRM.CRMServices.SMS.InterfaceExec { public class EntSms2 : IMsgSend { string account = ""; string pwd = ""; string url = ""; public EntSms2(string _account = "", string _password = "") { EntSmsConfig config = new EntSmsConfig(); account = config.account; pwd = config.pwd; url = config.url; if (!string.IsNullOrEmpty(_account) && !string.IsNullOrEmpty(_password)) { pwd = _password; account = _account; } } 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) { string mobilesStr = string.Join(",", mobiles); InterfaceModel.ReturnResult result = new InterfaceModel.ReturnResult(); result.SendStr = mobilesStr; try { WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); NameValueCollection postValues = new NameValueCollection(); webClient.Encoding = Encoding.UTF8; postValues.Add("command", "MT_REQ"); postValues.Add("userid", account); postValues.Add("pwd", pwd); postValues.Add("das", mobilesStr); postValues.Add("msgdc", "15"); postValues.Add("sc", "00"); postValues.Add("msg", encodeHexStr(msg)); byte[] responseArray = webClient.UploadValues(url, postValues); result.ReturnStr = Encoding.UTF8.GetString(responseArray); //command=MO_RES&userid=abc&mostat=ACCEPTD 成功 var dict_rst = new Dictionary(); foreach (var item in result.ReturnStr.Split('&')) { dict_rst.Add(item.Split('=')[0].Trim(), item.Split('=')[1].Trim()); } if (dict_rst["mtstat"] == "ACCEPTD") //最终用户接收 result.submitStatus = SubmitStatus.success; else result.submitStatus = SubmitStatus.fail; } catch (Exception ex) { result.submitStatus = SubmitStatus.fail; WX.CRM.Common.LogHelper.Error("EntSms.Send:" + ex.Message + ex.StackTrace); } return result; } public static string ToHex(string s, string charset, bool fenge) { if ((s.Length % 2) != 0) { s += "";//空格 } Encoding chs = Encoding.GetEncoding(charset); byte[] bytes = chs.GetBytes(s); string str = ""; for (int i = 0; i < bytes.Length; i++) { str += string.Format("{0:X}", bytes[i]); if (fenge && (i != bytes.Length - 1)) { str += string.Format("{0}", ","); } } return str.ToLower(); } #endregion public static String encodeHexStr(String realStr, int dataCoding = 15) { string strhex = ""; try { Byte[] bytSource = null; if (dataCoding == 15) { bytSource = Encoding.GetEncoding("GBK").GetBytes(realStr); } else if (dataCoding == 8) { bytSource = Encoding.BigEndianUnicode.GetBytes(realStr); } else { bytSource = Encoding.ASCII.GetBytes(realStr); } for (int i = 0; i < bytSource.Length; i++) { strhex = strhex + bytSource[i].ToString("X2"); } } catch { } return strhex; } //hex编码还原成字符 public static String decodeHexStr(String hexStr, int dataCoding = 15) { String strReturn = ""; try { int len = hexStr.Length / 2; byte[] bytSrc = new byte[len]; for (int i = 0; i < len; i++) { string s = hexStr.Substring(i * 2, 2); bytSrc[i] = Byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); } if (dataCoding == 15) { strReturn = Encoding.GetEncoding("GBK").GetString(bytSrc); } else if (dataCoding == 8) { strReturn = Encoding.BigEndianUnicode.GetString(bytSrc); } else { strReturn = System.Text.ASCIIEncoding.ASCII.GetString(bytSrc); } } catch { } return strReturn; } } }