为什么 HttpClient 总是给我相同的响应?

Why does the HttpClient always give me the same response?(为什么 HttpClient 总是给我相同的响应?)

本文介绍了为什么 HttpClient 总是给我相同的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:
VS2012 更新 4
Windows Phone 8 SDK
基于 WP OS 7.1 的全新 Windows Phone 项目
NuGet 包 1:MS 异步
NuGet 包 2:MS HttpClient


Environment:
VS2012 Update 4
Windows Phone 8 SDK
a fresh new Windows Phone project based on WP OS 7.1
NuGet pack1: MS Async
NuGet pack2: MS HttpClient


项目做什么
论坛 API 的单一测试:
在论坛中发布新主题并刷新 JSON 以检查计数"是否累积.


What does the project do
a single test for a forum API:
to post a new thread in the forum and refresh the JSON to check if the "count" is accumulate.


用户界面是什么意思

第一个文本块:
响应 json 将显示在此处

What is the UI mean

1st textblock:
the response json will display here

3 个按钮:
[刷新 JSON]:使用 API1 刷新线程列表
【新帖】:使用API​​2在论坛发帖,标题和文字会显示在下方
[IE]:在IE中打开json使用API​​1

3 buttons:
[refresh JSON]: use API1 to refresh the threads list
[New Post]: use API2 to post a new thread in the forum, title and text will display below
[IE]: open json in IE use API1

线程信息:
您刚刚发布的主题使用 [New Post] 按钮


Thread information:
the thread you've just post use [New Post] button


JSON 是什么意思
只需要关注 2 个部分
计数:244 ---> 论坛中的总线程数
"57923" ---> 第一个线程的标题(字段data"是线程列表的数组)


What does the JSON mean
only need to focus 2 parts
count: 244 ---> total threads count in the forum
"57923" ---> the title of the 1st thread (the field "data" is a array of the threads list)


测试程序是什么
1.打开应用
2.点击【刷新JSON】获取列表
3.记住第一个线程的计数"部分和标题"
4.点击【新帖】发新帖,帖的标题会显示在下方
5.点击【刷新JSON】查看最新线程列表
6. 预期:计数"累加 1,标题"部分应与下图相同


What is the test procedure
1. open the app
2. click [refresh JSON] to get the list
3. remember the "count" part and the "title" of the 1st thread
4. click [New Post] to post a new thread, the title of the thread will display below
5. click [refresh JSON] to see the latest threads list
6. expected: the "count" accumulate by 1 and the "title" part should be the same as below


问题:
1.点击【新帖】后,无论点击多少次【刷新JSON】,计数都不会累积!

2.但是在Chrome(桌面)打开url,你会发现json已经更新了!

3.关闭应用重新打开,点击【刷新JSON】,JSON会按预期更新,但是当你发布一个新线程时,问题又出现了


Problem:
1. after clicking the [New Post], No matter how many times you hit [refresh JSON], the count will not accumulate!

2. but open the url in Chrome(Desktop), you will find that the JSON has already been updated!

3. close the app and reopen it, hit [refresh JSON], the JSON will updated as expected, but when you post a new thread, the problem occurs again


为什么以及如何解决?


Why and How to solve it?


代码

private async void RefreshJSONButton_Click(object sender, RoutedEventArgs e)
{
    this.JsonView.Text = string.Empty;
    HttpClient client = new HttpClient();
    string url =
        "API1 Address Here";
    string x = await client.GetStringAsync(url);
    this.JsonView.Text = x;
    client.Dispose();
}

private async void NewPostButton_Click_1(object sender, RoutedEventArgs e)
{
    Random rnd = new Random();
    this.num = rnd.Next(1, 99999);

    // generate the title and content with random number
    string threadTitle = this.num.ToString();
    string threadContent = "111" + num.ToString();

    // generate the url
    string url =
        "API2 Address Here";
    url = url + "&title=" + threadTitle + "&content=" + threadContent;

    // use HttpClient to send the new thread
    HttpClient client = new HttpClient();
    string x = await client.GetStringAsync(url);
    client.Dispose();

    // display the thread informartion for further check
    this.titleView.Text = threadTitle;
    this.contentView.Text = threadContent;
}

推荐答案

看来,您在每次刷新点击时都点击了相同的 URL.有时 Windows Phone 将请求和响应存储在缓存中.

It seems, you are hitting the same URL on every refresh click. Sometime Windows Phone stores the request and response in cache.

如果可能,您可以在请求中再添加一个参数,然后在每次点击时更改此参数的值.您可以使用 GUID 或 Unix 时间戳.

If possible, you add one more parameter to your request and on every click just change the value of this parameter. You can use GUID or Unix Timestamp.

你可以试试:

 private async void RefreshJSONButton_Click(object sender, RoutedEventArgs e)
    {
        this.JsonView.Text = string.Empty;
        HttpClient client = new HttpClient();
    int unixTimestamp = (int)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

    string url =
    "http://API1 Address Here/Your queries&temp=unixTimestamp";
    string x = await client.GetStringAsync(url);
    this.JsonView.Text = x;
    client.Dispose();
    }

这篇关于为什么 HttpClient 总是给我相同的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 HttpClient 总是给我相同的响应?

基础教程推荐