Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is HttpClient's GetStringAsync is unbelivable slow?

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?

like image 308
Jason94 Avatar asked Nov 09 '13 21:11

Jason94


1 Answers

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);
like image 127
Jeff Haluska Avatar answered Oct 31 '22 14:10

Jeff Haluska