【问题标题】:Why does my HttpWebRequest return a 503?为什么我的 HttpWebRequest 返回 503?
【发布时间】:2018-08-28 15:30:47
【问题描述】:

所以我刚刚开始学习 HttpWebRequests 及其功能。 我已经到了想学习如何在 CookieContainer 中捕获 cookie 并解析它们的地步。

问题是某些网站返回 503 错误,我不确定。 本示例中将使用其中一个网站。 从我在线阅读的内容来看,这是一个 503 错误。

超文本传输​​协议 (HTTP) 503 服务不可用服务器 错误响应代码表示服务器尚未准备好处理 请求。

常见原因是服务器因维护而停机或 超载。此响应应用于临时条件和 如果可能,Retry-After HTTP 标头应包含估计的 服务恢复时间。

自从网站启动并运行以来,这似乎根本不适合。 为什么我的请求会返回 503 状态码,我应该如何以适当的方式解决此问题?

static void Main(string[] args)
        {

            //1. Create a HTTP REQUEST

            //Build the request
            Uri site = new Uri("https://ucp.nordvpn.com/login/");

            //Inizializing a new instance of the HttpWebRequest and casting it as a WebRequest
            //And calling the Create function and using our site as a paramter which the Create function takes.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(site);

            //Inizialize a new instance of the CookieContainer
            CookieContainer cookies = new CookieContainer();

            //The request has a CookieContainer, which is null by default, so we are just assinging the newly inizialized instance
            //of our CookieContainer to our requests CookieContainer
            request.CookieContainer = cookies;

            //Print out the number of cookies before the response (of course it will be blank)
            Console.WriteLine(cookies.GetCookieHeader(site));

            //Get the response and print out the cookies again
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine(cookies.GetCookieHeader(site));
            }

            Console.ReadKey();
        }

【问题讨论】:

  • HTTPS 正在使用中。搜索 ServicePointManager.SecurityProtocolServicePointManager.ServerCertificateValidationCallback。当您收到503 时,您应该检查接收流的响应主体(如果不为空)。应包含一条消息 (Html),以便您更好地了解问题所在。

标签: c# .net httpwebrequest httpwebresponse


【解决方案1】:

您尝试访问的 url 正在使用云托管,它使用许多安全措施,包括哪个浏览器正在访问该站点

为此,您需要更改 HttpWebRequest 的 userAgent 属性

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0";

【讨论】:

    【解决方案2】:

    您尝试访问的 URL 似乎受 CloudFlare 保护。如果没有一些额外的工作,您不能将基本的 HttpWebRequest 用于该类型的请求。虽然我没有尝试过,但您可以选择绕过这种保护: CloudFlareUtilities

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2017-07-04
      • 2013-04-18
      • 2020-06-06
      相关资源
      最近更新 更多