我如何绕过C#上的429个太多请求错误?

问题描述 投票:0回答:1

因此,我尝试使用c#google进行窃检查,经过几次调试后,我的代码不再起作用。我试图查看异常,并发现它是429 Too Many Requests error。我想做的是要么绕过此错误(因为我仍然可以从同一台PC访问google),要么花点时间再试一次。我该怎么办?

用于Google搜索的代码:

private void SearchAndWrite(string text)
        {

            string txtKeyWords = text;
            listBox1.Items.Clear();
            StringBuilder sb = new StringBuilder();
            byte[] ResultsBuffer = new byte[8192];
            string SearchResults = "http://google.com/search?q=" + txtKeyWords.Trim();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
            //request.Headers["X-My-Custom-Header"] = "'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)\\AppleWebKit / 537.36(KHTML, like Gecko) Cafari / 537.36'";

            try
            {
                int count = 0;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream resStream = response.GetResponseStream();

                count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);
                string tempString = null;
                do
                {
                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(ResultsBuffer, 0, count);
                        sb.Append(tempString);
                    }
                }

                while (count > 0);
                string sbb = sb.ToString();
                HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                html.OptionOutputAsXml = true;
                html.LoadHtml(sbb);
                HtmlNode doc = html.DocumentNode;
                foreach (HtmlNode link in doc.SelectNodes("//a[@href]"))
                {
                    //HtmlAttribute att = link.Attributes["href"];
                    string hrefValue = link.GetAttributeValue("href", string.Empty);
                    if (!hrefValue.ToString().ToUpper().Contains("GOOGLE") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))
                    {
                        int index = hrefValue.IndexOf("&");
                        if (index > 0)
                        {
                            hrefValue = hrefValue.Substring(0, index);
                            listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));
                        }
                    }
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString(), "An error has occurred!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }






        }
c# http search
1个回答
1
投票

虽然我看不到所有代码,也看不到任何递归,但我假设您正在多次调用SearchAndWrite,但是您可能需要限制查询的速率。

尝试在每个请求之间放置5或10秒的等待时间,如果问题消失了,那么您需要找到一种避免对Google进行查询的方法。

考虑使用队列和工作循环。

© www.soinside.com 2019 - 2024. All rights reserved.