【问题标题】:HttpWebRequest 429 error, any way to bypass?HttpWebRequest 429错误,有什么办法绕过?
【发布时间】:2020-02-13 11:34:52
【问题描述】:

我需要使用来自的 json 读取基于税务识别的信息 https://wl-api.mf.gov.pl/api/search/nip/5250007738?date=2020-02-13

还有工作代码示例

 private static void Main()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            Log("NIP: ");
            string nip = Console.ReadLine();

            string date = DateTime.Now.ToShortDateString();
            string url = "https://wl-api.mf.gov.pl/api/search/nip/{0}?date={1}";
            string fullUrl = string.Format(url, nip, date);

            var get = Get(fullUrl);

            RootObject account = JsonConvert.DeserializeObject<RootObject>(get);
            if (account != null)
                Log("Status: " + account.result.subject.statusVat);

            Log("Press any key to continue...");


            Console.ReadKey();
        }

        public static string Get(string url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception err)
            {
                Log(err.Message);
            }
            return string.Empty;
        }

        static void Log(string msg)
        {
            Console.WriteLine(msg);
        }

但是 API 有一些限制,我每天只能收到 10 个请求。但是,使用网络浏览器我可以不受任何限制地获得它。有什么办法可以绕过 429 限制吗?

【问题讨论】:

    标签: c# json httpwebrequest httpwebresponse http-status-code-429


    【解决方案1】:

    您可以通过欺骗您的 IP 或用户代理来绕过此限制,但您确实不应该这样做。

    如果服务器的 API 限制是每天 10 个请求,您应该遵守此限制。如果这是您的服务器,当然,您可以绕过它。但是,由于它不是您的服务器/API,请遵守其规则。

    有关 429 错误的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429

    【讨论】:

    • 好吧,我并不是真的想“ping”它 10xsec,只是有一天我可能需要使用它 5 次,再使用 15 次。顺便问一下,为什么我访问网站时没有限制?我可以按住 F5 刷新它并仍然得到正确的响应。
    • 这是因为您使用的是使用 HTTP 协议而不是 API 的 Web 浏览器访问该网站。同时,您的代码使用了网站的 API,这可能会限制使用。希望这可以澄清它。
    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多