gpt4 book ai didi

c# - 如何调用多个请求到 http 服务器

转载 作者:可可西里 更新时间:2023-11-01 16:41:21 27 4
gpt4 key购买 nike

我想不通,这是怎么回事!对不起,如果我确实误解了一些基本的东西 - 所以任何帮助表示赞赏!

场景:一个非常简单的 windows 10 通用应用程序必须调用一个 http 服务器,它只发送回简单的 JSON 对象。我使用 Windows.Web.Http 包中的 HttpClient 编写了这个应用程序。该应用程序在 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里面,好像一切正​​常!没有异常(exception)或任何东西。但结果始终显示为第一次调用的结果(例如,来自服务器的时间戳仍然相同)。

有人知道吗?
我完全迷路了。

最佳答案

天哪,找到了:Windows.Web.Http是个错误,你只需要使用System.Net.Http代替。一切都运行得很顺利。奇怪的!并感谢所有帮助!

关于c# - 如何调用多个请求到 http 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com