为什么HttpClient的总是给我同样的反应? [英] Why does the HttpClient always give me the same response?

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

问题描述

环境:结果
VS2012更新4结果
的Windows Phone 8 SDK结果
基于WP OS 7.1结果一个全新的Windows Phone项目
的NuGet PACK1:MS异步结果
的NuGet PACK2:MS HttpClient的搜索结果结果

什么项目做结果
一个论坛API一个测试:结果
张贴在论坛上一个新的线程并刷新JSON来检查计数的积累。结果搜索结果

什么是UI的意思结果
结果
第一个文本块:结果
响应JSON将在这里显示结果

3个按钮:结果
[刷新JSON]:使用API​​1刷新线程列表结果
[新主题]:使用API​​2以在论坛上发帖,标题一个新线程,文字会显示如下结果
[IE]:开放JSON在IE中使用API​​1结果

主题的信息:结果
线程你刚刚发布使用[新主题]按钮,结果搜索结果

什么的JSON意味着结果
只需要关注2零件|
数:244 --->线程总在论坛结果算
57923---> 1号线的标题(领域的数据是线程列表的数组)结果搜索结果

什么是测试程序结果
1.打开应用程序结果
2.单击[刷新JSON]以获取列表结果
3.记得伯爵的一部分和1号线结果的称号
4.单击[新主题]发布一个新线程,该线程的标题会显示如下结果
5.单击[刷新JSON]看到最新的主题列表结果
预计6:将计数增加1积累和标题部分应该是相同的,如下结果搜索结果

问题:结果
1.点击[新主题],不管你打多少次后[刷新JSON],计数不会累积!搜索结果
2.但在Chrome中打开(桌面)的网址,你会发现,JSON已经更新!搜索结果
3.关闭应用程序,并重新打开它,命中[刷新JSON],如预期的JSON会更新,但是当你发布一个新的线程,再次出现搜索结果搜索

问题

为什么和怎样解决呢?搜索结果搜索

的code 搜索

 专用异步无效RefreshJSONButton_Click(对象发件人,RoutedEventArgs E)
{
    this.JsonView.Text =的String.Empty;
    HttpClient的客户端=新的HttpClient();
    字符串的URL =
        API1地址在这里;
    字符串x =等待client.GetStringAsync(URL);
    this.JsonView.Text = X;
    client.Dispose();
}私人异步无效NewPostButton_Click_1(对象发件人,RoutedEventArgs E)
{
    随机RND =新的随机();
    this.num = rnd.Next(1,99999);    //生成标题和内容与随机数
    字符串threadTitle = this.num.ToString();
    串threadContent =111+ num.ToString();    //生成URL
    字符串的URL =
        API2地址在这里;
    URL =网址+&放大器;标题=+ threadTitle +&放大器; CONTENT =+ threadContent;    //使用的HttpClient发送新的线程
    HttpClient的客户端=新的HttpClient();
    字符串x =等待client.GetStringAsync(URL);
    client.Dispose();    //显示进一步的检查线程informartion
    this.titleView.Text = threadTitle;
    this.contentView.Text = threadContent;
}


解决方案

看来,你打在每次刷新时点击同一个URL。有时,Windows Phone的存储请求,并在高速缓存中的响应。

如果可能的话,你一个参数添加到您的请求,并在每次点击只需更改这个参数的值。您可以使用GUID或Unix时间戳。

您可以尝试这样的:

 专用异步无效RefreshJSONButton_Click(对象发件人,RoutedEventArgs E)
    {
        this.JsonView.Text =的String.Empty;
        HttpClient的客户端=新的HttpClient();
    INT unixTimestamp =(INT)(DateTime.Now.Subtract(新的DateTime(1970,1,1)))TotalSeconds。    字符串的URL =
    HTTP:// API1地址在这里,/你的查询和放大器; TEMP = unixTimestamp;
    字符串x =等待client.GetStringAsync(URL);
    this.JsonView.Text = X;
    client.Dispose();
    }

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


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.


What is the UI mean

1st textblock:
the response json will display here

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

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


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)


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


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?


The code

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;
}

解决方案

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

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.

You can try like:

 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的总是给我同样的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆