0

所以,我试图做一个抄袭检查器,c#经过google几次调试,我的代码不再工作了。我试图查看异常并发现它是一个429 Too Many Requests error. 我想要做的是绕过这个错误(因为我仍然可以从同一台电脑访问谷歌)或者有时间我可以再试一次。我怎样才能做到这一点?

搜索代码:

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

            

            

            
        }
4

1 回答 1

1

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

尝试在每个请求之间等待 5 或 10 秒,如果问题消失,那么您需要找到一种方法,不要用查询来打击谷歌。

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

于 2020-01-30T12:18:25.737 回答