更新數據後,當調用web服務時,它仍會提取舊數據。只有當我註銷應用程序然後再次登錄時,纔會加載新數據。HttpWebRequest和HttpWebResponse顯示舊數據
protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
parameterValue = this.NavigationContext.QueryString["parameter"];
Uri UserDetailUrl = new Uri(Constants.WebService.ws_single_user + "?user_id=" + parameterValue);
HttpWebRequest UserDetailRequest = (HttpWebRequest)HttpWebRequest.Create(UserDetailUrl);
HttpWebResponse UserDetailResponse = (HttpWebResponse)await UserDetailRequest.GetResponseAsync();
StreamReader reader = new StreamReader(UserDetailResponse.GetResponseStream());
string UserDetailString = reader.ReadToEnd();
reader.Close();
XDocument XUserDetailDoc = XDocument.Load(new StringReader(UserDetailString));
if (((XElement)XUserDetailDoc.Element("main")).Element("result").Value == "success")
{
txtEmail.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("email").Value;
txtFirstName.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("fname").Value;
txtLastName.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("lname").Value;
txtMobile.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("mobile").Value;
}
}
Windows 8的手機應用 - 使用C#和XAML
的GetResponseAsync如下:
public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
{
var taskComplete = new TaskCompletionSource<HttpWebResponse>();
request.BeginGetResponse(asyncResponse =>
{
try
{
HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
taskComplete.TrySetResult(someResponse);
}
catch (WebException webExc)
{
HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
taskComplete.TrySetResult(failedResponse);
}
}, request);
return taskComplete.Task;
是的,它確實有幫助..這可能是最好的答案..因爲我完全控制了服務器端,所以我簡單地在那裏添加了頭部(「Cache-Control」,「no-cache」)。 非常感謝 –