Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange HttpClient result

Tags:

c#

wpf

httpclient

When trying to get data from a Hitbox API I get a strange result. For a one API's command it happens everytime, for the other only sometimes. The result more or less like that (this is the last result I have got):

\u001f�\b\0\0\0\0\0\0\u0003콋w�8������s�Μmu�~��s��N�t&��N�g6��C��Ͷ$z%9igf��[(�\u0005�\u000f��(:�6����\u000f\b P�C\u0015\n�\u007f�V�\u007f�d��h����2_�&��Nj,�����S������q�\u0017�7��\u0019<�n�~��YoVY�\u0018M>�S�kP���|^���w������9�w2��\u0605�4�Ƿ�&\u0015�ƛ��Xo�\u0014[�6w\u0011onVP\u0005��e\u000e��\u05ca�l�/����⇝��%�u�u�s�����=���k�w��z\u0003�a����SR,���s�1����ůdž��2~.6\u0006T>��fR��l�(���\u0017�GHoV�&/�m#�'\u0013�C�N/��E|Q�\u0012���3+�6\u0003z\u0012���q>�{�_��eW�7\u0016�rsIw\u0012\u0018&\u0017��V�\u000f�Ŀ\u001f������e�\u0002A��Zg��U\u0006��\\g\u0015�VP��u.E8Hj�LA���/͋��|�����;xk\u001e��ǣZ�\3y��\u00019\u0017���ī\u0002��ڜ���u����O]v���XA�{�\u0004�K�l.o\u0016�%t\u0006�<{̆O��=�\u0017\u0017\u0015%$G��\"Oϡ�\u007f����^̹hH���q�8�\u0001�6�\u0015�y�{��S��4+�pb-\\eI�bB]^\u001f��{��jՊx�\u0004��\u0001�º��QD��\u001eK\u0001;�\u0002+��Y���!�\a��\u001a��(()>e��ש�r0T?��\u001f�Q���5t�R���� \u0005\u001f(���l\u0013�\v�\r-�\n�U?߭_��&�l>�\r0(N4))/���uc��3�\<�U\u0013\u001f\u0002ȱ���^n6ד��g�/�ʹ��ͧ��rP����\u001f���\f4y~\u0005=�V3�\u001c;�k\u0002}�'�m\u001cc�oG��_\u0003b�4�`��

It's much much longer but it's pointless to copy all of it (about 30000 characters).

My code that I use to get the json result is:

string result;

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(hitboxApiLink);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = client.GetAsync(requestString).Result;
    result = response.Content.ReadAsStringAsync().Result;
}

return result;

I used this before:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(Path.Combine(hitboxApiLink, requestString));
request.KeepAlive = false;
request.ContentType = "application/json; charset=utf-8";
WebResponse response = request.GetResponse();

using(StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
    result = reader.ReadToEnd();
}

And it returns the same. But when I try to get data with Google Chrome or Internet Explorer it returns normal result. The api is: http://developers.hitbox.tv/

The link I try to get data from is: https://api.hitbox.tv/media/live/list

like image 704
Kuba Wasilczyk Avatar asked Apr 29 '16 13:04

Kuba Wasilczyk


1 Answers

The response is GZIPped (even though this wasn't specified by an Accept-Encoding header in the request, so is technically a fault on the server).

Your second example can be fixed by adding the following line before you fire off the request:

request.AutomaticDecompression = DecompressionMethods.GZip;

This should give you everything you need to figure out how to decompress the response of the HttpClient version.

like image 174
spender Avatar answered Nov 14 '22 07:11

spender