【问题标题】:Error 405 Method not Allowed on WebRequestWebRequest 上不允许使用错误 405 方法
【发布时间】:2021-01-07 16:42:05
【问题描述】:

我正在尝试从以下页面获取页面代码。它给了我一个 405 错误。如果我尝试从主页获取页面代码,它可以正常工作,但从这个特定页面我得到 Method not allowed,想法?

        WebRequest request = WebRequest.Create("https://www.realtor.com/realestateandhomes-search/California/counties");
        request.UseDefaultCredentials = true;
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();

        Console.WriteLine(responseFromServer);

【问题讨论】:

标签: c# webrequest


【解决方案1】:

网站认为你是机器人。

详情:

我用HttpClient 进行了尝试(推荐:在收到非 200 响应代码时不会抛出异常),并检查了响应 HTML。这是重要的片段:

      <p>
        As you were browsing, something about your browser made us think you might be a bot. There are a few reasons this might happen, including:
      </p>
      <ul>
        <li>You're a power user moving through this website with super-human speed</li>
        <li>You've disabled JavaScript and/or cookies in your web browser</li>
        <li>A third-party browser plugin is preventing JavaScript from running. Additional information is available in this
          <a title='Third party browser plugins that block javascript' href='http://ds.tl/help-third-party-plugins' target='_blank'>
            support article
          </a>.
        </li>
      </ul>

如果您想要完整的响应,请尝试运行:

async void LogResponse()
{
    using System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    var response = await client.GetAsync("https://www.realtor.com/realestateandhomes-search/California/counties");
    Console.WriteLine(await response.Content.ReadAsStringAsync());
}

对 realtor.com 的侧面投诉,405Request-Line 中指定的方法是不允许的)是一个相当糟糕的响应代码; 403服务器理解请求,但拒绝满足它。)似乎更适合。

【讨论】:

  • 谢谢。我确实添加了 User-Agent 标头并开始工作,但现在我收到了您在上面提到的错误,认为我是一个机器人。有趣的是它仍然可以在浏览器中使用。那么有什么不同。浏览器和代码之间?如何让它认为我仍然是浏览器?
  • 这可能具有挑战性,因为您的 dotnet 应用程序有点像机器人。我想看看像cors-anywhere 这样的东西是否可以帮助你。
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 2017-11-13
  • 2016-12-02
  • 2012-10-25
  • 2012-03-21
  • 2015-11-25
  • 1970-01-01
  • 2012-08-24
相关资源
最近更新 更多