Windows phone 7 为什么DownloadStringCompleted方法在WindowsPhone7中总是返回相同的结果?

Windows phone 7 为什么DownloadStringCompleted方法在WindowsPhone7中总是返回相同的结果?,windows-phone-7,Windows Phone 7,在WindowsPhone7中,DownloadStringCompleted方法总是返回相同的结果,我们面临一个问题 首先通过服务绑定挂起的请求。显示列表框中的RequTest放置了两个按钮。当单击调用的accept服务时,该服务将更新表。 再次调用挂起的请求服务时显示以前的结果。为什么请告诉我 代码: private void getpendingrequests() { WebClient wcgetfriends=新WebClient(); wcgetfriends.DownloadSt

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

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

代码:

private void getpendingrequests()
{
WebClient wcgetfriends=新WebClient();
wcgetfriends.DownloadStringAsync(
新Uri(“http://{ipaddress}/Network/Reccords/GetFriends?userid=“+userid”);
wcgetfriends.DownloadStringCompleted+=
新的下载StringCompletedEventHandler(
wcgetfriends_下载完成);
}
void wcgetfriends_下载字符串已完成(对象发送方,
下载StringCompletedEventArgs(e)
{
字符串resultgetfriends=null,responseCode=null;
使用(变量读取器=新的StringReader(e.Result))
{
resultgetfriends=reader.ReadToEnd();
}
XmlReader xmlDoc=XmlReader.Create(newmemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(resultgetfriends));
而(xmlDoc.Read())
{
if(xmlDoc.NodeType==XmlNodeType.Element)
{
if(xmlDoc.Name.Equals(“响应代码”))
{
responseCode=xmlDoc.ReadInnerXml();
}
}
}
if(转换为32(响应代码)==200)
{
字符串result1=e.Result.ToString();
XDocument xmlDocu=XDocument.Load(新的MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(result1));
利息=(来自xmlDocu.substands(“朋友”)中的rts)
选择new SampleCheckedData
{
Id=(字符串)rts.Element(“userid”),
名称=(字符串)rts.Element(“名称”),
Icon=(字符串)rts.Element(“imageurl”),
}).ToObservableCollection();
this.lstfriendrequest.ItemsSource=利率;
}
if(转换为32(响应代码)==201)
{
Show(“UserId不是整数类型”);
}
if(转换为32(响应代码)==202)
{
MessageBox.Show(“用户不存在”);
}
if(转换为32(响应代码)==203)
{
MessageBox.Show(“无未决请求”);
}
}
私有void requestaccept()
{
WebClient wcacceptrequest=新的WebClient();
wcacceptrequest.DownloadStringAsync(
新Uri(“http://{ipaddress}/Network/Reccords/FriendRequestAcceptance?userid=“+userid+”&frienduserid=“+\u id+”&acceptid=“+1”);
wcacceptrequest.DownloadStringCompleted+=
新的下载StringCompletedEventHandler(
wcacceptrequest_下载完成);
图像b=发送者作为图像;
var res=interestrates.Where(a=>a.Id.Equals(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag)).ToList();
如果(res.Count==1)
删除(res.First());
}
void wcacceptrequest_下载字符串已完成(对象发送方,
下载StringCompletedEventArgs(e)
{
字符串resultacept=null,responseCode=null;
使用(变量读取器=新的StringReader(e.Result))
{
resultacept=reader.ReadToEnd();
}
XmlReader xmlDoc=XmlReader.Create(新内存流(System.Text.UTF8Encoding.UTF8.GetBytes(resultacept));
而(xmlDoc.Read())
{
if(xmlDoc.NodeType==XmlNodeType.Element)
{
if(xmlDoc.Name.Equals(“响应代码”))
{
responseCode=xmlDoc.ReadInnerXml();
}
}
}
if(转换为32(响应代码)==200)
{
lstfriendrequest.ItemsSource=“”;
利率=新的可观察集合();
bindGetFriends();
}
if(转换为32(响应代码)==201)
{
Show(“UserId不是整数类型”);
}
if(转换为32(响应代码)==202)
{
MessageBox.Show(“朋友Id不是整数类型”);
}
if(转换为32(响应代码)==203)
{
Show(“接受id不是整数类型”);
}
}

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

这几乎肯定是由于手机或某些网络代理中的缓存造成的

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

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

以下是其他类似的问题和答案:


既然在
e.Result
中已经有字符串,为什么要创建
StringReader
来获取字符串?为什么要多次将响应代码转换为整数?为什么要以不同的方式对XML文档进行两次解析?为什么要对字符串调用
ToString
?你好像要疯了
                WebClient wcgetfriends = new WebClient();
                wcgetfriends.DownloadStringCompleted += wcgetfriends_DownloadStringCompleted;
                wcgetfriends.DownloadStringAsync(
                    new Uri(
                        "http://{ipaddress}/Network/Reccords/GetFriends?userid=" 
                        + userId 
                        + "&ignored=" 
                        + DateTime.UtcNow.Ticks));