-1

我正在尝试使用 httpwebrequest\webclient 从站点获取数据,所以我正在做的是每 30 秒发送一个请求以获取站点的 html。

发生的事情是该站点阻止我进行拒绝服务攻击,因为我从计算机发送了太多请求。

如果不每 30 秒获取一次数据,我如何知道网站上何时有新数据?

或者

如何每 30 秒从站点获取数据而不会因拒绝服务攻击而被阻止?

好的,所以我添加了一些代码:

public void DownloadFile(String remoteFilename, String localFilename)
{
            Stream remoteStream = null;
            Stream localStream = null;
            HttpWebRequest gRequest = (HttpWebRequest)WebRequest.Create(remoteFilename);
            gRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 GTBDFff GTB7.0";

            gRequest.CookieContainer = new CookieContainer();
            gRequest.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, */*";
            gRequest.KeepAlive = true;
            gRequest.ContentType = @"application/x-www-form-urlencoded";


            #region CookieManagement
            if (gCookies != null && gCookies.Count > 0)
            {
                gRequest.CookieContainer.Add(gCookies);
            }

            HttpWebResponse gResponse;

            try{
                gResponse = (HttpWebResponse)gRequest.GetResponse();

                //check if the status code is http 200 or http ok

                if (gResponse.StatusCode == HttpStatusCode.OK)
                {
                    remoteStream = gResponse.GetResponseStream();
                    localStream = File.Create(localFilename);
                    byte[] buffer = new byte[1024];
                    int bytesRead;

                    do
                    {
                        // Read data (up to 1k) from the stream
                        bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                        // Write the data to the local file
                        localStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);
                }
                else
                {
                    MessageBox.Show("Error!");
                    Application.Exit();
                }

                if (gResponse != null) gResponse.Close();
                if (remoteStream != null) remoteStream.Close();
                if (localStream != null) localStream.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                Application.Exit();
            }
            #endregion
        }

在计时器中:

DownloadFile("http://www.fxp.co.il/forumdisplay.php?f=2709", @"C:\tmph.html");

所以这个论坛是一个买卖论坛,所以我想做的是每 30 秒获取一次论坛 html,使用 htmlagilitypack 检查 html 中未读“购买”帖子的数量。

4

1 回答 1

1

您可以使用更长的轮询间隔并执行 HEAD 请求以避免获取整个文档。您可以解析返回的标头,并且仅在它与前一个标头不同时才执行 GET。

于 2012-05-02T21:15:53.807 回答