为什么在我的C#winforms应用程序中第一次HttpClient.PostAsync调用极其缓慢?

编程入门 行业动态 更新时间:2024-10-26 02:36:01
本文介绍了为什么在我的C#winforms应用程序中第一次HttpClient.PostAsync调用极其缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个像这样的httpclient:

I have an httpclient like this :

var client = new HttpClient();

我这样张贴:

var result = client.PostAsync( endpointUri, requestContent);

并获得如下响应:

HttpResponseMessage response = result.Result;

我知道此调用将阻塞线程,这就是线程应该如何工作的(只需为自己构建工具,不需要异步线程即可)

I understand this call will block the thread, thats how its supposed to work (just building a tool for myself, no async threads needed)

我第一次运行此呼叫,大约需要2分钟才能得到结果.同时,如果我在其他地方进行完全相同的调用,则它会在200毫秒内完成.即使我点击了google,也需要2分钟.但是,在第一个通话之后,只要我保持应用程序打开,任何其他通话都可以.当我打开应用程序时,它只是第一次校准.可能是什么原因造成的?

The first time I run this call, it takes about 2 minutes to get a result. Meanwhile, if I do the exact same call elsewhere its done in 200ms. Even if I hit google, it takes 2 minutes. But, after the first call, as long as I keep the app open any additional calls are good. Its just the first cal when I open the application. What could be causing this?

推荐答案

问题是,它在尝试为客户端解析代理时挂了很长时间.像这样初始化HttpClient可以达到目的:

The problem was that it was hanging for a very long time trying to resolve a proxy for the client. Initializing the HttpClient like this did the trick:

var client = new HttpClient(new HttpClientHandler { UseProxy = false });

更多推荐

为什么在我的C#winforms应用程序中第一次HttpClient.PostAsync调用极其缓慢?

本文发布于:2023-11-08 22:06:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   缓慢   winforms   PostAsync   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!