为什么不这样直接取指定网页的内容?
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
解决方案 »
- C#调用COM接口,出现AccessViolationException异常,求助
- 可不可以让 dataGridView绑定一个List?
- 下面的是不是叫做反射?能否举个例子?
- 急,希望能指导一下
- 求一正则表达式对字符串的取值
- *******在线等!!!!!!!!1如何在winform让datagridview的列名(不是单元格的内容)居中显示?**********
- .net2005如果在实现?
- DataGridView自带的Combobox使用方法
- 在VS2008里用C#如何开发三层式WindowsForms程序?
- 关于自定义构造函数,有点不明,上来请教下
- webBrowser 怎么断开登录状态
- WEB 1:1客服系统,原理是什么?
有状态的
还有当前WebBrowser的uri是不是http://www.skyunion.net/book.php
当前WebBrowser的uri已经跳转到了http://www.skyunion.net/book.php页面
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);这个方法,如果("网页地址")需要验证,则无法取到该网页的内容啊
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
这2句换个位置看下呢
换下位子不对啊
我读取网页内容的语句写在this.button3_Click中
如果换了顺序完全不对了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;
using System.IO;
namespace GetWebHtml
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
} static public void writeFile(string path, string str)
{ //如果文件path存在就打开,不存在就新建 .append 是追加写, CreateNew 是覆盖
FileStream fst = new FileStream(path, FileMode.CreateNew);
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8")); //写入
swt.WriteLine(str);
swt.Close();
fst.Close(); } private void button1_Click(object sender, EventArgs e)
{
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
Cursor.Current = Cursors.WaitCursor;
axWebBrowser1.Navigate(textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
Cursor.Current = Cursors.Default;
} private void button2_Click(object sender, EventArgs e)
{
String url = "http://fengchao.baidu.com/fc-mc/spread/preview.do?userid=1851750&keyword=%E7%BE%8E%E5%AE%B9&area=2";
//String url = "http://www.skyunion.net/book.php"; System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
//Cursor.Current = Cursors.WaitCursor; axWebBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
//Cursor.Current = Cursors.Default; this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
} private void button3_Click(object sender, EventArgs e)
{ IHTMLDocument2 HTMLDocument = (IHTMLDocument2)axWebBrowser1.Document; string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
string[] arHtml = strHtml.Split('\n'); writeFile("out.txt", strHtml);
} private void button4_Click(object sender, EventArgs e)
{
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("http://www.skyunion.net/");
string s = System.Text.Encoding.UTF8.GetString(pageData);
writeFile("out.txt", s);
} }
}
我想通过这个去访问一个页面,然后把那个页面的内容保存下来
要保存的那个页面需要验证
你的axWebBrowser1.DownloadComplete应该是失败了
你在button3_Click里面调试下他的状态看看
1.webBrowser1.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr); 先导航到需要保存的那个页面
2. IHTMLDocument2 HTMLDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
string[] arHtml = strHtml.Split('\n'); writeFile("out.txt", strHtml);
再来保存需要保存的内容
用两个按钮来做可以
并到一个里面读出来就是错的
把
this.axWebBrowser1.DownloadComplete += new System.EventHandler(this.button3_Click);
改成
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.button3_Click);
即可
应该是DocumentCompleted 事件,而不是DownloadComplete 事件
再次谢谢各位!