如何向http服务器调用多个请求

时间:2016-02-21 09:31:05

标签: c# http windows-10-universal

我无法弄清楚,这里发生了什么!对不起,如果我错过了一些基本的东西 - 所以任何帮助赞赏!

场景:一个非常简单的Windows 10通用应用程序必须调用一个http服务器,它只返回简单的JSON对象。 我使用Windows.Web.Http包中的HttpClient编写了这个App。 应用程序在MainPage.xaml上只有一个按钮,操作在MainPage.xaml.cs中。尽可能简单。 现在:我从按钮回调方法(private void button_Click(object sender, RoutedEventArgs e))调用异步方法,如下所示:

private async void callServer() {
    HttpClient Xclient = new HttpClient();
    Uri uri = new Uri("http://www.thinkitdifferent.de:8080/json/showAll");

    string result;
    try {

        result = await Xclient.GetStringAsync(uri);
        Debug.WriteLine("full result: >" + result + "<");
    }
    catch (Exception ex) {
        // Details in ex.Message and ex.HResult.       
        Debug.WriteLine("Error: " + ex.Message);
    }

    // Once your app is done using the HttpClient object call dispose to 
    // free up system resources (the underlying socket and memory used for the object)

    Xclient.Dispose();

    // Once your app is done using the HttpClient object call close to 
    // free up system resources (the underlying socket and memory used for the object)
    // httpClient.close();
}

行为现在是:当我第一次调用方法时,一切都很好。服务器只显示请求,并将正确的信息发送到应用程序。

但是当我将其称为秒及以后所有时 - 服务器不会显示任何活动。即使使用tcpdump,我也没有在端口8080上的服务器上看到任何内容(我尝试使用端口80进行相同的操作,结果相同)。

但现在真的很奇怪:在App中,一切似乎都没问题!没有例外或任何事情。但结果总是显示为第一次调用的结果(例如服务器的时间戳仍然相同)。

任何人都有任何想法?
我完全迷失了。

1 个答案:

答案 0 :(得分:0)

omg,发现它: Windows.Web.Http是错误,你只需要使用 System.Net.Http代替。 一切都顺利进行。奇怪!并感谢所有的帮助!