9
votes

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

1
A little off topic, but please don't use .Result and use the await keywoard instead. Using .Result can cause a deadlock if you're not being careful enough with the context and is overall not a good practise when you can use await to unpack the result. Don't Block on Async CodeKerim Emurla
Path.Combine is for file-system paths. Abusing it for use with URLs will probably lead to trouble.spender
@KerimEmurla Ok thanks I will change that.Kuba Wasilczyk
@spender I'm not using this code anymore. It was mostly to eliminate double slash.Kuba Wasilczyk
Include the response headers in your questionjgauffin

1 Answers

15
votes

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.