简体   繁体   English

WebRequest 上不允许使用错误 405 方法

[英]Error 405 Method not Allowed on WebRequest

I am trying to grab the page code from the below page.我正在尝试从下面的页面中获取页面代码。 It gives me a 405 error.它给了我一个 405 错误。 If I try to get the page code from the home page it works fine but from this specific page i get Method not allowed, thoughts?如果我尝试从主页获取页面代码,它可以正常工作,但从这个特定页面我得到 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);

The site thinks you are a bot.该网站认为您是机器人。

Details:细节:

I tried it with HttpClient (recommended: doesn't throw an exception upon receiving a non-200 response code), and inspected the response HTML.我用HttpClient尝试过(推荐:在收到非 200 响应代码时不抛出异常),并检查了响应 HTML。 Here is the important snipit:这是重要的片段:

      <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>

If you want the full response, try running this:如果您想要完整的响应,请尝试运行以下命令:

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

Side complaint against realtor.com, 405 ( The method specified in the Request-Line is not allowed ) is a rather poor response code for this;对 realtor.com, 405的侧面投诉(不允许在请求行中指定的方法)是一个相当糟糕的响应代码; a 403 ( The server understood the request, but is refusing to fulfill it. ) seems better suited.一个403服务器理解请求,但拒绝满足它。 )似乎更适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM