首页 > 解决方案 > 为什么我会收到与 WebClient 不兼容的浏览器消息?

问题描述

我正在尝试使用 WebClient 使用 .NET 抓取网页。但是我不断收到不兼容的浏览器消息。请参阅下面的 HTML 源代码(不幸的是,我无法共享链接,因为它是内部的)。不知何故,服务器认为我使用的是不兼容版本的 IE。我尝试设置用户代理,但没有任何效果。

<!--[if lt IE 10]>

    <div id="incompatible_ie" style="display:block;font-family:Calibri,sans-serif;font-size:14px;text-align:center;margin:20% 0 5em;color:#777">

      <div style="font-size:xx-large;margin-bottom:2em">HOME</div>

      <div>Your browser is not supported by the application.<br>We recommend you use Google Chrome or IE11 without Compatibility Mode.</div>

    </div>

    <script>

      console && console.error("Unsupported browser: Removing #root element to prevent React from loading.");

      document.getElementsByTagName("body")[0].removeChild(document.getElementById("root"));

    </script>

    <![endif]-->

C#

WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763");
string html = client.DownloadString(path);

标签: c#http-headerswebclientmicrosoft-edgedotnet-httpclient

解决方案


推荐阅读