我们正面临一个问题,即DownloadStringCompleted方法总是在Windows Phone 7中返回相同的结果?
首先通过service绑定挂起的请求。显示listbox中的requuest放置了两个按钮。当点击该服务更新表的调用接受服务时。 再次呼叫待处理的请求服务显示以前的结果。为什么请告诉我...
代码:
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方法中得到以前的结果......
答案 0 :(得分: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));
还有其他类似的问题和答案: