为什么在我的C#winforms应用程序中第一次HttpClient.PostAsync调用极其缓慢? [英] Why is first HttpClient.PostAsync call extremely slow in my C# winforms app?

查看:134
本文介绍了为什么在我的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调用极其缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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