我有这段代码从WP8的API获取JSON数据:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(mainURL.ToString());
HttpResponseMessage response = await client.GetAsync("");
string res = await response.Content.ReadAsStringAsync();
var root = await JsonConvert.DeserializeObjectAsync<Rootobject>(res);
一切都运行良好,但是当我从网站更新Web API上的数据并尝试使用此代码再次检索数据时,即使在浏览器中访问URL,它也会获取新数据。
当我逐行调试时,我看到“response”对象包含旧数据。
我找到解决此问题的唯一方法是以这种方式重建项目。
如何正确获取更新数据?
答案 0 :(得分:1)
我有同样的问题。我试过这可能对你有帮助。
HttpWebRequest request = HttpWebRequest.CreateHttp(mainURL.ToString());
request.Method = "GET or Post";
request.BeginGetResponse(ResponseCallBack, request);
void ResponseCallBack(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
using (Stream data = response.GetResponseStream())
{
using (var reader = new StreamReader(data))
{
string jsonString = reader.ReadToEnd();
MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject yourdata= dataContractJsonSerializer.ReadObject(memoryStream) as Rootobject;
}
}
}
答案 1 :(得分:1)
可能涉及一些缓存。尝试在URL中添加一些随机字符串,例如
client.BaseAddress = new Uri(mainURL.ToString()+"&random="+DateTime.Now.Ticks);