<i id='XaqCx'><tr id='XaqCx'><dt id='XaqCx'><q id='XaqCx'><span id='XaqCx'><b id='XaqCx'><form id='XaqCx'><ins id='XaqCx'></ins><ul id='XaqCx'></ul><sub id='XaqCx'></sub></form><legend id='XaqCx'></legend><bdo id='XaqCx'><pre id='XaqCx'><center id='XaqCx'></center></pre></bdo></b><th id='XaqCx'></th></span></q></dt></tr></i><div id='XaqCx'><tfoot id='XaqCx'></tfoot><dl id='XaqCx'><fieldset id='XaqCx'></fieldset></dl></div>

  1. <tfoot id='XaqCx'></tfoot>
  2. <small id='XaqCx'></small><noframes id='XaqCx'>

        <bdo id='XaqCx'></bdo><ul id='XaqCx'></ul>
    1. <legend id='XaqCx'><style id='XaqCx'><dir id='XaqCx'><q id='XaqCx'></q></dir></style></legend>
    2. HttpClient 请求类似浏览器

      HttpClient Request like browser(HttpClient 请求类似浏览器)
      • <bdo id='OiGFI'></bdo><ul id='OiGFI'></ul>
        <legend id='OiGFI'><style id='OiGFI'><dir id='OiGFI'><q id='OiGFI'></q></dir></style></legend>
        <i id='OiGFI'><tr id='OiGFI'><dt id='OiGFI'><q id='OiGFI'><span id='OiGFI'><b id='OiGFI'><form id='OiGFI'><ins id='OiGFI'></ins><ul id='OiGFI'></ul><sub id='OiGFI'></sub></form><legend id='OiGFI'></legend><bdo id='OiGFI'><pre id='OiGFI'><center id='OiGFI'></center></pre></bdo></b><th id='OiGFI'></th></span></q></dt></tr></i><div id='OiGFI'><tfoot id='OiGFI'></tfoot><dl id='OiGFI'><fieldset id='OiGFI'></fieldset></dl></div>

        <small id='OiGFI'></small><noframes id='OiGFI'>

          <tbody id='OiGFI'></tbody>

              1. <tfoot id='OiGFI'></tfoot>

                本文介绍了HttpClient 请求类似浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我通过 HttpClient 类调用网站 www.livescore.com 时,我总是收到错误500".可能是服务器阻止了来自 HttpClients 的请求.

                When I calling site www.livescore.com by HttpClient class I always getting error "500". Probably server blocked request from HttpClients.

                1)还有其他方法可以从网页中获取html吗?

                1)There is any other method to get html from webpage?

                2)如何设置标题以获取 html 内容?

                2)How I can set the headers to get html content?

                当我像在浏览器中一样设置标题时,我总是得到 stange 编码的内容.

                When I set headers like in browser I always get stange encoded content.

                    http_client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
                    http_client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
                    http_client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
                    http_client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
                

                3) 我该如何解决这个问题?有什么建议吗?

                3) How I can slove this problem? Any suggestions?

                我在 C# 和 HttpClientClass 中使用 Windows 8 Metro Style App

                I using Windows 8 Metro Style App in C# and HttpClientClass

                推荐答案

                给你 - 注意你必须解压缩 gzip 编码的结果 - 你得到的结果 按照 mleroy:

                Here you go - note you have to decompress the gzip encoded-result you get back as per mleroy:

                private static readonly HttpClient _HttpClient = new HttpClient();
                
                private static async Task<string> GetResponse(string url)
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
                    {
                        request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
                        request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
                        request.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
                        request.Headers.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
                
                        using (var response = await _HttpClient.SendAsync(request).ConfigureAwait(false))
                        {
                            response.EnsureSuccessStatusCode();
                            using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                            using (var decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress))
                            using (var streamReader = new StreamReader(decompressedStream))
                            {
                                return await streamReader.ReadToEndAsync().ConfigureAwait(false);
                            }
                        }
                    }
                }
                

                这样调用:

                var response = await GetResponse("http://www.livescore.com/").ConfigureAwait(false); // or var response = GetResponse("http://www.livescore.com/").Result;
                

                这篇关于HttpClient 请求类似浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                set equality in linq(在 linq 中设置相等)
                HashSet conversion to List(HashSet 转换为 List)
                How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                • <bdo id='Vond2'></bdo><ul id='Vond2'></ul>
                    • <i id='Vond2'><tr id='Vond2'><dt id='Vond2'><q id='Vond2'><span id='Vond2'><b id='Vond2'><form id='Vond2'><ins id='Vond2'></ins><ul id='Vond2'></ul><sub id='Vond2'></sub></form><legend id='Vond2'></legend><bdo id='Vond2'><pre id='Vond2'><center id='Vond2'></center></pre></bdo></b><th id='Vond2'></th></span></q></dt></tr></i><div id='Vond2'><tfoot id='Vond2'></tfoot><dl id='Vond2'><fieldset id='Vond2'></fieldset></dl></div>

                      1. <tfoot id='Vond2'></tfoot>

                          <small id='Vond2'></small><noframes id='Vond2'>

                            <tbody id='Vond2'></tbody>
                          <legend id='Vond2'><style id='Vond2'><dir id='Vond2'><q id='Vond2'></q></dir></style></legend>