Why is HttpClient's GetStringAsync is unbelivable slow?

Jason94 picture Jason94 · Nov 9, 2013 · Viewed 8.4k times · Source

I have a Windows Phone 8 project where I've taken to use the PCL (Portable Class Library) project too since I'm going to build a Win8 app to.

However, while calling my api (in Azure) my HttpClient's GetStringAsync is so slow. I threw in a couple of debugs with datetime and GetStringAsync took like 14 seconds! And sometimes it takes longer.

What I'm doing is retrieving simple JSON from my Azure API site. My Android client has no problem with getting that same data in a split second... so is there something I'm missing?

The setup is pretty straight forward:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Token", "something");
string responseJSON = await client.GetStringAsync("url");

I've places the debug times right before and after the await there, in between it is 14 seconds!

Does someone know why?

Answer

Jeff Haluska picture Jeff Haluska · Jul 9, 2016

I had the same problem, and found this question. The problem for me is that the HttpClient attempts to use a proxy, but for most people the proxy doesn't exist. This is what makes it slow. Change the initialization to the following and you will notice a significant speed up.

HttpClientHandler hch = new HttpClientHandler();
hch.Proxy = null;
hch.UseProxy = false;

HttpClient client = new HttpClient(hch);