コード例 #1
0
        public void ProcessRequest(HttpContext context)
        {
            /*第一步:请求代理商接口*/
            bool value = false;
            string message = "Fail!";
            XElement result = new XElement("Result");
            IAgentFriends friendsClass = new Normal();
            StringBuilder friendUserName = new StringBuilder();
            HttpContext current = HttpContext.Current;
            string getFriendsBbsXml = friendsClass.FriendsString(current.Request.Params["Uid"]);  //从代理商获取返回来的XML文档
            DataSet Ds = new DataSet();
            if (getFriendsBbsXml != "")
            {
                try
                {
                    Ds.ReadXml(new System.IO.StringReader(getFriendsBbsXml));
                    for (int i = 0; i < Ds.Tables["item"].DefaultView.Count; i++)
                    {
                        friendUserName.Append(Ds.Tables["item"].DefaultView[i]["UserName"].ToString() + ",");
                    }
                }
                catch (Exception ex)
                {
                    if (log.IsErrorEnabled)
                        log.Error("Get Table Item ", ex);
                }
            }
            if ((friendUserName.Length <= 1)||(getFriendsBbsXml==""))
            {
                result.Add(new XAttribute("value", value));
                result.Add(new XAttribute("message", message));
                context.Response.ContentType = "text/plain";
                context.Response.Write(result.ToString(false));
                return;
            }

            /*第二步:将好友以4000为一个段落,切割成查询条件*/
            string[] friends = friendUserName.ToString().Split(',');                          //将当前的全部用户全部切割成数组
            ArrayList condictArray = new ArrayList();
            StringBuilder tempString = new StringBuilder(4000);
            for (int i = 0; i < friends.Count(); i++)
            {
                if (friends[i] == "")
                {
                    break;
                }
                if (tempString.Length + friends[i].Length < 4000)                             //以4000个长度为一个查询条件
                {
                    tempString.Append(friends[i] + ',');
                }
                else
                {
                    condictArray.Add(tempString.ToString());                                  //查询条件
                    tempString.Remove(0, tempString.Length);
                }
            }
            condictArray.Add(tempString.ToString());                                          //最尾部补上剩余的

            /*第三步:查询数据库中的数据*/
            try
            {
                for (int i = 0; i < condictArray.Count; i++)
                {
                    string temp = condictArray[i].ToString();
                    using (PlayerBussiness db = new PlayerBussiness())
                    {
                        FriendInfo[] friendsResult = db.GetFriendsBbs(temp);
                        for (int j = 0; j < friendsResult.Count(); j++)
                        {
                            DataRow[] dr = Ds.Tables["item"].Select("UserName='******'");
                            XElement node = new XElement("Item",
                                    new XAttribute("NickName", friendsResult[j].NickName),
                                    new XAttribute("UserName", friendsResult[j].UserName),
                                    new XAttribute("UserId", friendsResult[j].UserID),
                                    new XAttribute("Photo", dr[0]["Photo"] == null ? "" : dr[0]["Photo"].ToString()),
                                    new XAttribute("PersonWeb", dr[0]["PersonWeb"] == null ? "" : dr[0]["PersonWeb"].ToString()),
                                    new XAttribute("IsExist", friendsResult[j].IsExist),
                                    new XAttribute("OtherName", dr[0]["OtherName"] == null ? "" : dr[0]["OtherName"].ToString())
                                    );
                            result.Add(node);
                        }
                    }
                }
                value = true;
                message = "Success!";
            }
            catch (Exception ex)
            {
                log.Error("IMFriendsGood", ex);
            }
            result.Add(new XAttribute("value", value));
            result.Add(new XAttribute("message", message));
            context.Response.ContentType = "text/plain";
            context.Response.Write(result.ToString(false));
        }