Why is HttpClient's GetStringAsync is unbelivable slow?
Asked Answered
K

2

14

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?

Kentonkentucky answered 9/11, 2013 at 21:49 Comment(7)
Do you have other async methods called in the call stack? Are you always awaiting them? Sometimes a slow app means you are using .Result somewhere instead of awaiting. For example string responseJSON = client.GetStringAsync("url").Result;.Sauveur
How big is the string your Uri is returning? Have you tried using for instance WebClient on WP8, if there is major speed difference? What about speed of the response when using simply in web browser?Stairhead
@chue x im awaiting all of them, but at the moment im just running that one. tried using. Result takes forever!Kentonkentucky
@Martin Suchan the string is 2,99 KB "big". Ive a plain WP8 project with WebClient and there it works fine! Why not in the PCL using HttpClient?Kentonkentucky
Are you using WIFI or Cellular (same network as android)? I use WebRequest as I can mock it during unit testing.Chokeberry
Are you measuring this under the debugger? The debugger will slow down during module loads as it tries to fetch symbols which can skew your measurements. Try measuring on the device without the debugger. If you can reproduce your perf issue with a snippet of code that isn't using your private Azure site please share and I'd be happy to investigate.Adulthood
You asked this question 3 years ago, but I'm running into this exact problem today. Did you ever find a solution?Coughlin
W
14

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);
Weighted answered 9/7, 2016 at 5:20 Comment(0)
V
0

Replace GetStringAsync with GetString to ensure it's actually async\await causing your issue and not anything else in the stack.

Virility answered 24/4, 2015 at 17:33 Comment(2)
There is no GeString method on HttpClient if I can recall correctly.Livvy
You are correct. Got confused with WebClient: msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx It has an one and an async equivalent. It will help to eliminate the httpclient as a potential issue.Virility

© 2022 - 2024 — McMap. All rights reserved.