Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is first HttpClient.PostAsync call extremely slow in my C# winforms app?

I have an httpclient like this :

var client = new HttpClient();

I post to it like this:

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

And get the response like this:

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)

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?

like image 485
Zach Avatar asked Jan 29 '16 12:01

Zach


1 Answers

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
            });
like image 57
Zach Avatar answered Sep 20 '22 18:09

Zach