怎么设置webBrowser的HTML内容
展开全部
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials; //获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(tbUrl.Text); //从指定网站下载数据
string pageHtml = Encoding.Default.GetString(pageData); //把字节转化为字符串,注意编码方式
2、使用WebBrowser控件
//通过WebBrowser空间访问网页,然后获取网页数据
WebBrowser web = new WebBrowser();
web.Navigate(tbUrl.Text);
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted); //当网页加载完成时触发该事件,获取网页数据
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//获取该html页面内的Table标签的内容
WebBrowser web = (WebBrowser)sender;
HtmlElementCollection ElementCollection = web.Document.getElementsByTagName_r("Table");
foreach (HtmlElement item in ElementCollection)
{
textBox1.AppendText(item.InnerText + "\n");
}
}
3、使用HttpWebRequest/HttpWebResponse
HttpWebRequest httpReq;
HttpWebResponse httpResp;
Uri httpURL = new Uri(this.tbUrl.Text);
httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
httpResp = (HttpWebResponse)httpReq.GetResponse();
Stream respStream = httpResp.GetResponseStream(); //得到数据流,再把数据流转化为特定的数据(图片,文本。。。)
//如果是图片流
pictureBox1.Image = Image.FromStream(respStream);
//如果是文本流
byte[] cbuffer = new byte[256];
StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8); //编码为utf8
byteRead = respStreamReader.Read(cbuffer,0,256); //读取256个字节
string strResp = new string(cbuffer,0,byteRead);
MyWebClient.Credentials = CredentialCache.DefaultCredentials; //获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(tbUrl.Text); //从指定网站下载数据
string pageHtml = Encoding.Default.GetString(pageData); //把字节转化为字符串,注意编码方式
2、使用WebBrowser控件
//通过WebBrowser空间访问网页,然后获取网页数据
WebBrowser web = new WebBrowser();
web.Navigate(tbUrl.Text);
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted); //当网页加载完成时触发该事件,获取网页数据
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//获取该html页面内的Table标签的内容
WebBrowser web = (WebBrowser)sender;
HtmlElementCollection ElementCollection = web.Document.getElementsByTagName_r("Table");
foreach (HtmlElement item in ElementCollection)
{
textBox1.AppendText(item.InnerText + "\n");
}
}
3、使用HttpWebRequest/HttpWebResponse
HttpWebRequest httpReq;
HttpWebResponse httpResp;
Uri httpURL = new Uri(this.tbUrl.Text);
httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
httpResp = (HttpWebResponse)httpReq.GetResponse();
Stream respStream = httpResp.GetResponseStream(); //得到数据流,再把数据流转化为特定的数据(图片,文本。。。)
//如果是图片流
pictureBox1.Image = Image.FromStream(respStream);
//如果是文本流
byte[] cbuffer = new byte[256];
StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8); //编码为utf8
byteRead = respStreamReader.Read(cbuffer,0,256); //读取256个字节
string strResp = new string(cbuffer,0,byteRead);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询