奇怪的HttpClient结果 [英] Strange HttpClient result

查看:60
本文介绍了奇怪的HttpClient结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从Hitbox API中获取数据时,我得到了一个奇怪的结果.对于一个API的命令,它每次都会发生,而对于另一个API则有时会发生.结果大致如此(这是我得到的最后结果):

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. 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 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 /x | ; xk \u001e ǣZ \3y \ u00019 \u0017 ī\u0002 ڜ u O]v XA{{\u0004 K lo\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 \

\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�`��

要长得多,但是复制所有内容(大约30000个字符)毫无意义.

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

我用来获取json结果的代码是:

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;

我以前曾经用过这个

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();
}

它返回相同的结果.但是,当我尝试使用Google Chrome或Internet Explorer获取数据时,它将返回正常结果.该api是: http://developers.hitbox.tv/

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/

我尝试从中获取数据的链接是: https://api.hitbox.tv/媒体/直播/列表

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

推荐答案

响应已通过GZIPped处理(即使请求中的Accept-Encoding标头未指定响应,从技术上讲也是服务器故障).

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;

这应该为您提供您需要的一切以了解如何解压缩响应HttpClient版本.

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

这篇关于奇怪的HttpClient结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆