Why is the HttpClient GetStringAsync incredibly slow?

I have a Windows Phone 8 project in which I started using the PCL (Portable Class Library) project, since I'm going to create a Win8 application.

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

What I am doing is getting simple JSON from my Azure API site. My Android client has no problem getting the same data in a split second ... so am I missing something?

The setup is pretty simple:

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 put the debug time right before and after waiting there, between 14 seconds!

Does anyone know why?

+6
source share
2 answers

I had the same problem and I found this question. The problem for me is that HttpClient is trying to use a proxy server, but for most people, a proxy server does not exist. This is what makes it slow down. Change the initialization to the next and you will notice a significant speed.

 HttpClientHandler hch = new HttpClientHandler(); hch.Proxy = null; hch.UseProxy = false; HttpClient client = new HttpClient(hch); 
+4
source

Replace GetStringAsync with GetString to make sure that async \ await is actually causing your problem, not something else on the stack.

-1
source

Source: https://habr.com/ru/post/957756/


All Articles