C# 如何单击浏览器";停止“;使用WatiN的按钮

C# 如何单击浏览器";停止“;使用WatiN的按钮,c#,watin,C#,Watin,我正在点击某个页面上的链接,它会在“新建”选项卡中打开。但该网站的加载速度不快,并且有一些flash动画和其他一些加载时间较长的元素。但所有我需要的信息都会立即加载。所以我需要让页面停止加载。浏览器的“停止”按钮会很好。我可以用WatiN访问它吗??还是有别的办法解决我的问题?谢谢您尝试过使用httpwebrequest/webresponse吗?它应该只获取没有任何其他元素的页面(没有css,没有图像,没有flash动画)-只有页面代码,没有其他内容 private void GetPage(

我正在点击某个页面上的链接,它会在“新建”选项卡中打开。但该网站的加载速度不快,并且有一些flash动画和其他一些加载时间较长的元素。但所有我需要的信息都会立即加载。所以我需要让页面停止加载。浏览器的“停止”按钮会很好。我可以用WatiN访问它吗??还是有别的办法解决我的问题?谢谢

您尝试过使用httpwebrequest/webresponse吗?它应该只获取没有任何其他元素的页面(没有css,没有图像,没有flash动画)-只有页面代码,没有其他内容

private void GetPage(string protocol, string address, string extraPath, string[][] postData)
{
      try
      {
           long length = 0;
           string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(protocol + address + extraPath);
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest2.Method = "POST";
            httpWebRequest2.KeepAlive = true;
            httpWebRequest2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpWebRequest2.Host = address;
            httpWebRequest2.Referer = protocol + address;
            httpWebRequest2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30";
            httpWebRequest2.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
            httpWebRequest2.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,pt-PT;q=0.2,en-GB;q=0.2");
            httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Stream memStream = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes(boundary + "\r\n");
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            length += boundarybytes.Length;
            boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            length += boundarybytes.Length;

            // Send any data via POST that you might need
            for (int i = 0; i < postData.length; i++;
            {
                 string head = "Content-Disposition: form-data; name=\"" + postData[i][0] + "\"\r\n\r\n";
                 byte[] headbytes = System.Text.Encoding.ASCII.GetBytes(head);
                 memStream.Write(headbytes, 0, headbytes.Length);
                 length += headbytes.Length;
                 head = postData[i][1];
                 headbytes = System.Text.Encoding.ASCII.GetBytes(head);
                 memStream.Write(headbytes, 0, headbytes.Length);
                 length += headbytes.Length;
                 memStream.Write(boundarybytes, 0, boundarybytes.Length);
                 length += boundarybytes.Length;
            }

            httpWebRequest2.ContentLength = memStream.Length;
            Stream requestStream = httpWebRequest2.GetRequestStream();
            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();
            WebResponse webResponse2 = httpWebRequest2.GetResponse();
            Stream stream2 = webResponse2.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2, Encoding.UTF8);
            string htmlpage = reader2.ReadToEnd();
            reader2.Close();
            webResponse2.Close();
            httpWebRequest2 = null;
            webResponse2 = null;
            // At this point, htmlpage contains all the code from the page ready to be handled
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                HttpWebResponse response = ex.Response as HttpWebResponse;
                if (response != null)
                {
                    // Got an error page from the server so save it and show it on browser
                    Stream stream2 = response.GetResponseStream();
                    StreamReader reader2 = new StreamReader(stream2, Encoding.UTF7);
                    StreamWriter writer = new StreamWriter("error.html");
                    writer.Write(reader2.ReadToEnd());
                    writer.Close();
                    reader2.Close();
                    response.Close();
                    response = null;
                    System.Diagnostics.Process.Start("error.html");
                }
            }
        }
    }

这是一个相当复杂的使用示例-但由于我对您的需求细节没有任何线索,我将其全部放在了一起-根据您的需求,您可能不需要其中的一些-希望它能有所帮助-实际上我还需要其他东西。当我的页面完全加载时,它会启动javascript。我需要这个javascript来工作。但此页面加载速度非常慢,有时会冻结。所以javascript不会启动。当我按下浏览器中的“停止”按钮时,它会启动那个javascript。这正是我需要的。你有什么建议吗?谢谢,除非javascript在一个单独的文件中(而不是在标记下的html页面中),否则上面应该这样做——只需在catch上复制error page在那里所做的事情,并将其应用到htmlpage变量的内容。但是,如果javascript位于页面外部,那么您也必须获取javascript文件(相同的方法)根据其在站点上的位置放置它-以相同的方式保存页面,然后运行页面(与错误消息相同)当然,这是假设该网站允许您下载javascript文件——这并不总是我尝试过的情况,但对我来说似乎有点复杂。但我找到了解决办法。我下载了firefox的AdBlock Plus扩展,并创建了一些规则。所以它封锁了我所有的内容,只留下我需要的框架。不过谢谢你的回复。
GetPage("http://", "www.google.com", "/search?sourceid=chrome&ie=UTF-8&q=stackoverflow", new strin[] {new string[]}); // in this case i send no data for post