c#关于webBrowser的获取网页源代码

f8520525 2009-07-16 09:35:23
本人想用c# 做一个程序打开网页可以获取网页的源代码 用webBrowser控件 但是获取不了网页的源代码 ,网上大部分都是用vb做的,可是我不会vb 难道用c#就不能实现吗? 急请各位高手 交哈小弟 最好能写几句源代码 谢谢
...全文
2350 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
辞旧.迎新 2009-12-04
  • 打赏
  • 举报
回复
1楼、6楼、10楼均可实现
Snowdust 2009-08-15
  • 打赏
  • 举报
回复
Encoding encoding = Encoding.GetEncoding(webBrowser.Document.Encoding);
StreamReader stream = new StreamReader(webBrowser.DocumentStream, encoding);
string source = stream.ReadToEnd();
benzite 2009-07-17
  • 打赏
  • 举报
回复
查onNewWindowEvent,onDocumentComplete
yanlong521521 2009-07-17
  • 打赏
  • 举报
回复
mark 下
纯纯璐璐 2009-07-17
  • 打赏
  • 举报
回复
我刚做完这个东西,贴的代码测试通过。
纯纯璐璐 2009-07-17
  • 打赏
  • 举报
回复

public WebResponse DoGet(string url)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "get";
webRequest.CookieContainer = this.cookies;
return webRequest.GetResponse();
}
catch (Exception)
{

throw;
}

}

//根据响应返回字符串
public string GetHtml(WebResponse response)
{
try
{
StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.Default);
string strHtml = stream.ReadToEnd();
stream.Close();
return strHtml;
}
catch (Exception)
{

throw;
}

}

冷月孤峰 2009-07-17
  • 打赏
  • 举报
回复
1楼兄弟已经给出代码了,楼主为什么不试一下再问
opcdde 2009-07-17
  • 打赏
  • 举报
回复
找到那个链接地址
用webbrowser 的Navigate方法
然后用webbrowser获得源文件

webbrowser.Document.Body.InnerHtml这个属性就是源文件
f8520525 2009-07-17
  • 打赏
  • 举报
回复
呵呵 谢谢大哥指点 我还想请教大哥的是 我通过webBrowser打开的那个网页有个超链接 点击超链接 是一个弹出窗口 我想得到弹出窗体里面的信息并下载下来 如何通过C#实现?要求源代码 谢谢大哥指点 谢谢拉
PandaIT 2009-07-16
  • 打赏
  • 举报
回复
// 难道用c#就不能实现吗?

//这话别轻易的乱说
PandaIT 2009-07-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Net;

namespace Test
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine(getHtml("http://www.baidu.com"));

}

private static string getHtml(string url)
{

WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(url);
return Encoding.Default.GetString(myDataBuffer);

}
}
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧