【问题标题】:C# HttpClient request fails to scrape (both on System.Net and Windows.Web http requests)C# HttpClient 请求无法抓取(在 System.Net 和 Windows.Web http 请求上)
【发布时间】:2019-03-06 22:17:38
【问题描述】:

我正在尝试抓取此网站上的新闻:https://www.livescore.com/soccer/news/

using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient())
            {
                    client.DefaultRequestHeaders.Add("User-Agent",
                                 "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");
                using (Windows.Web.Http.HttpResponseMessage response = await client.GetAsync(new Uri(pageURL)))
                using (Windows.Web.Http.IHttpContent content = response.Content)
                {
                    try
                    {
                        string result = await content.ReadAsStringAsync();
                        Debug.WriteLine(result);
            }
        }
        }

我看到我收到了一个包含 Your browser is out of date or some of its features are disabledresponse 我搬到Windows.Web 添加certificates 因为我在UWP 并尝试添加以下certificates

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.WrongUsage);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationFailure);

但我仍然从服务器获得相同的response。 知道如何绕过这个吗?

编辑:他们确实有旧的服务器,不安全,http://www.livescore.com/,我想我可以抓取所有内容,但没有新闻。

【问题讨论】:

    标签: c# http filter httprequest httpclient


    【解决方案1】:

    我认为问题在于用户代理字符串。您告诉网站您使用的浏览器是 Internet Explorer 10。 查看此页面http://www.useragentstring.com/pages/useragentstring.php?name=Internet+Explorer 并尝试使用 Internet Explorer 11 的用户代理(在此之前从您的 ie11 浏览器打开页面以检查该功能是否正常)

    【讨论】:

    • 这不是骗局。该网站告诉您该网站看到的浏览器太旧。这是因为使用用户代理参数,您告诉站点调用是由 Internet Explorer 10 浏览器进行的。如果您更改用户代理参数,您告诉网站该请求是由 Internet Explorer 11 发出的,并且该网站认为浏览器已更新并支持所有功能
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多