3

我们面临一个问题,DownloadStringCompleted 方法总是在 windows phone 7 中返回相同的结果?

首先通过服务绑定挂起的请求。显示列表框中的请求放置了两个按钮。当单击该调用接受服务时,该服务更新表。再次调用显示先前结果的待处理请求服务。为什么请告诉我...

代码:

private void getpendingrequests()
{
 WebClient wcgetfriends = new WebClient();
                    wcgetfriends.DownloadStringAsync(
                    new Uri("http://{ipaddress}/Network/Reccords/GetFriends?userid=" + userId));
                    wcgetfriends.DownloadStringCompleted +=
                     new DownloadStringCompletedEventHandler(
                                    wcgetfriends_DownloadStringCompleted);
}



 void wcgetfriends_DownloadStringCompleted(object sender,
                 DownloadStringCompletedEventArgs e)
        {

            String resultgetfriends = null, responseCode = null;
            using (var reader = new StringReader(e.Result))
            {
                resultgetfriends = reader.ReadToEnd();
            }
            XmlReader xmlDoc = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(resultgetfriends)));

            while (xmlDoc.Read())
            {
                if (xmlDoc.NodeType == XmlNodeType.Element)
                {

                    if (xmlDoc.Name.Equals("ResponseCode"))
                    {
                        responseCode = xmlDoc.ReadInnerXml();

                    }

                }

            }
            if (Convert.ToInt32(responseCode) == 200)
            {

                string result1 = e.Result.ToString();

                XDocument xmlDocu = XDocument.Load(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(result1)));

                interestrates = (from rts in xmlDocu.Descendants("Friend")

                                 select new SampleCheckedData
                                 {

                                     Id = (string)rts.Element("userid"),
                                     Name = (string)rts.Element("name"),
                                     Icon = (string)rts.Element("imageurl"),

                                 }).ToObservableCollection<SampleCheckedData>();


                this.lstFriendRequuest.ItemsSource = interestrates;

            }
            if (Convert.ToInt32(responseCode) == 201)
            {
                MessageBox.Show("UserId is not type of integer");
            }
            if (Convert.ToInt32(responseCode) == 202)
            {
                MessageBox.Show("User not exists");
            }
            if (Convert.ToInt32(responseCode) == 203)
            {
                MessageBox.Show("No Pending Requests");
            }

        }

private void requestaccept()
{
 WebClient wcacceptrequest = new WebClient();
                    wcacceptrequest.DownloadStringAsync(
                    new Uri("http://{ipaddress}/Network/Reccords/FriendRequestAcceptance?userid=" + userId + "&frienduserid=" + _id + "&acceptid=" + 1));
                    wcacceptrequest.DownloadStringCompleted +=
                     new DownloadStringCompletedEventHandler(
                                    wcacceptrequest_DownloadStringCompleted);
                    Image b = sender as Image;
                    var res = interestrates.Where(a => a.Id.Equals(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag)).ToList();
                    if (res.Count == 1)
                        interestrates.Remove(res.First());
}

   void wcacceptrequest_DownloadStringCompleted(object sender,
                DownloadStringCompletedEventArgs e)
        {

            String resultaccept = null, responseCode = null;
            using (var reader = new StringReader(e.Result))
            {
                resultaccept = reader.ReadToEnd();
            }
            XmlReader xmlDoc = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(resultaccept)));

            while (xmlDoc.Read())
            {
                if (xmlDoc.NodeType == XmlNodeType.Element)
                {

                    if (xmlDoc.Name.Equals("ResponseCode"))
                    {
                        responseCode = xmlDoc.ReadInnerXml();

                    }

                }

            }
            if (Convert.ToInt32(responseCode) == 200)
            {

                lstFriendRequuest.ItemsSource = "";
                interestrates = new ObservableCollection<SampleCheckedData>();
                bindGetFriends();

            }
            if (Convert.ToInt32(responseCode) == 201)
            {
                MessageBox.Show("UserId is not type of integer");
            }
            if (Convert.ToInt32(responseCode) == 202)
            {
                MessageBox.Show("Friend Id not type of integer");
            }
            if (Convert.ToInt32(responseCode) == 203)
            {
                MessageBox.Show("Accept id not type of integer");
            }

        }

请告诉我为什么在 bindfriends 方法中得到以前的结果......

4

1 回答 1

3

这几乎可以肯定是由手机或某些网络代理中的缓存引起的。

为了防止缓存,您可以尝试做几件事 - 如果您想始终禁用缓存,那么最快的方法之一就是在 GET 请求的末尾添加一个唯一编号 - 例如

                WebClient wcgetfriends = new WebClient();
                wcgetfriends.DownloadStringCompleted += wcgetfriends_DownloadStringCompleted;
                wcgetfriends.DownloadStringAsync(
                    new Uri(
                        "http://{ipaddress}/Network/Reccords/GetFriends?userid=" 
                        + userId 
                        + "&ignored=" 
                        + DateTime.UtcNow.Ticks));

还有其他类似的问题和答案:

于 2012-06-13T07:17:07.353 回答