public async Task <SMSMessageItem[]> GetMessage() { string result = string.Empty; try { string para = String.Format("U={0}&P={1}&S={2}&F={3}", _u, _p, _s, _f); string fullUrl = String.Format("{0}?{1}", "http://qxt.mpower100.com:8001/GetSM.aspx", para); //HttpWebRequest request = WebRequest.Create(fullUrl) as HttpWebRequest; //request.Method = "GET"; //request.Timeout = 30000; //request.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; //request.KeepAlive = true; //request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/msword, */*"; //request.Headers.Add("Accept-Encoding", "gzip, deflate"); //request.Headers.Add("Accept-Language", "zh-cn"); //HttpWebResponse response = request.GetResponse() as HttpWebResponse; //byte[] content = new byte[1024]; //Stream rspStream = response.GetResponseStream(); result = await Kugar.Core.Network.WebHelper.Create(fullUrl).Get_StringAsync(); //result = "<?xml version=\"1.0\" encoding=\"GB2312\" ?><RecvMsg><Return>OK</Return><Msg><SendNum>13682867081</SendNum><RecvNum>10657020801264</RecvNum><RecvTime>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</RecvTime><Content>C#145417#1656#15</Content></Msg></RecvMsg>"; //判断是否含有有效回复格式关键字 if (!result.Contains("<RecvMsg>")) { return(null); } XmlDocument doc = new XmlDocument(); doc.LoadXml(result); var node = doc.SelectNodes("//Msg"); if (node.Count <= 0) { return(null); } var query = from n in doc.SelectNodes("//Msg").Cast <XmlNode>() select n; var lst = new List <SMSMessageItem>(query.Count()); foreach (var q in query) { var item = new SMSMessageItem(); item.Mobile = q["SendNum"].InnerText; item.ReceiveDt = q["RecvTime"].InnerText.ToDateTime(); item.Message = q["Content"].InnerText.ToStringEx(); lst.Add(item); } return(lst.ToArray()); } catch (Exception ex) { LoggerManager.GetLogger("SMSMessageLog").Error(ex.Message); return(null); } }
/// <summary> /// 轮询获取短信 /// </summary> /// <returns></returns> public async Task <SMSMessageItem[]> GetMessage() { var url = "http://sms2.eachwe.com/api.php"; //string loginName = _config.AppSettings.GetValueByName<string>("Eachwe_LoginName"); //string passWord = _config.AppSettings.GetValueByName<string>("Eachwe_PassWord"); //var datas = new Dictionary<string, string>(); //datas.Add("username", _loginName); //datas.Add("password", _passWord); //datas.Add("method", "getreply"); //var respData = WebHelper.GetUriDataByPost(url, datas, Encoding.GetEncoding("gb2312")); var respData = await WebHelper.Create(url) .Encoding(Encoding.GetEncoding("gb2312")) .ContentType(WebHelper.ContentTypeEnum.FormUrlencoded) .SetParamter("method", "getreply") .SetParamter("username", _loginName) .SetParamter("password", _passWord) .Post_StringAsync(); ; var document = new XmlDocument(); if (!document.SafeLoadXML(respData)) { return(null); } var errors = document.GetElementsByTagName("error"); if (errors == null || errors.Count <= 0 || errors[0].InnerText.ToStringEx().Trim() != "0") { return(null); } var repliesItemList = document.GetElementsByTagName("reply"); if (repliesItemList == null || repliesItemList.Count <= 0) { return(null); } var lst = new List <SMSMessageItem>(repliesItemList.Count); foreach (XmlElement replyItem in repliesItemList) { var smsItem = new SMSMessageItem(); smsItem.Mobile = replyItem.GetChildNodeInnerText("mobile").ToStringEx(); smsItem.ReceiveDt = replyItem.GetChildNodeInnerText("datetime").ToDateTime("yyyy-MM-dd HH:mm").GetValueOrDefault(DateTime.MinValue); var msg = replyItem.GetChildNodeInnerText("msg").ToStringEx(); //msg = msg.Substring(10,msg.Length-11); smsItem.Message = msg; lst.Add(smsItem); } return(lst.ToArray()); }