有一个网站用webBrowser控件打开很慢,我要经常刷新有没有重要的东西并在网页上做一些操作(选择/输入/提交等).
我想用HttpWebRequest/HttpWebResponse 来定时与网站交互,这样由于不用下载大量图片等,并且读到的是HTML字符串格式,分析处理起来很方便,一旦发现满足需要了再用webBrowser打开.
获取网站HTML很容易,已经实现. private string GetHTMLstring(string url)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream stream;
byte[] byteRequest = { }; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); //加载URL
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length; //设置Header stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length); //POST信息
stream.Close(); webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //收到应答
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);
return (sr.ReadToEnd());
}
现在我如果判断有了需要的东西后,我不想再用webBrowser.Navigate再加载了(因为很慢可能就来不及了),我用webBrower.DocumentText(s) //s是GetHTMLstring返回的网页HTML的字符串.但是这样的话,点击webBrowser中的按钮什么的也无法完成网页的功能了. 请问如何能将2者组合起来满足我的需求呢?谢谢!
我想用HttpWebRequest/HttpWebResponse 来定时与网站交互,这样由于不用下载大量图片等,并且读到的是HTML字符串格式,分析处理起来很方便,一旦发现满足需要了再用webBrowser打开.
获取网站HTML很容易,已经实现. private string GetHTMLstring(string url)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream stream;
byte[] byteRequest = { }; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); //加载URL
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length; //设置Header stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length); //POST信息
stream.Close(); webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //收到应答
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);
return (sr.ReadToEnd());
}
现在我如果判断有了需要的东西后,我不想再用webBrowser.Navigate再加载了(因为很慢可能就来不及了),我用webBrower.DocumentText(s) //s是GetHTMLstring返回的网页HTML的字符串.但是这样的话,点击webBrowser中的按钮什么的也无法完成网页的功能了. 请问如何能将2者组合起来满足我的需求呢?谢谢!
这样呢?
{
string html = "";
html += "<html><head></head><body>";
html += "<input value=\"OK\" type=\"button\" onclick=\"alert('OK');\" name=\"OK\" id=\"OK\" />";
html += "</body></html>"; webBrowser1.DocumentText = html;
}private void button2_Click(object sender, EventArgs e)
{
HtmlDocument htmlDocument = webBrowser1.Document;
HtmlElement htmlElement = htmlDocument.GetElementById("OK");
htmlElement.InvokeMember("click");
}