C#不用WebBrowser直接下载网页HTML源码
发布网友
发布时间:2022-04-29 02:26
我来回答
共6个回答
热心网友
时间:2022-04-20 21:38
最简单的用WebClient:
调用方法:string html=DownloadData("http://www.baidu.com",Encoding.GetEncoding("gb2312"));
public static string DownloadData(string url,Encoding encoding)
{
WebClient web = new WebClient();
return encoding.GetString(web.DownloadData(url));
}
复杂一点用HttpWebRequest/HttpWebResponse:
调用方法:string html=DownloadHtmlPage("http://www.baidu.com",Encoding.GetEncoding("gb2312"),"GET",20);
public static string DownloadHtmlPage(string pageUrl, Encoding encoding, string requestMethod,int timeOut)
{
string value = string.Empty;
HttpWebResponse response=null;
Stream data=null;
StreamReader sr=null;
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(pageUrl);
request.Method = requestMethod;
if (timeOut != -1) request.Timeout = timeOut;
response = (HttpWebResponse)request.GetResponse();
data = response.GetResponseStream();
sr = new StreamReader(data, encoding);
string str;
StringBuilder source = new StringBuilder();
while ((str = sr.ReadLine()) != null)
source.Append(str).Append("\r\n");
value = source.ToString();
}
finally
{
if (sr != null) sr.Close();
if(data!=null) data.Close();
if(response!=null) response.Close();
}
return value;
}
热心网友
时间:2022-04-20 22:56
可以直接用WebClient。具体的参考MSDN,比自己用HttpWebRequest和HttpWebResponse简单许多。但是在需要Cookies的场合,还是使用后者比较可靠一点,或自己重写WebClient。
热心网友
时间:2022-04-21 00:31
你获得的源码都是html超文本标记语言,不会得到ASP JSP PHP代码的
源码有那么容易获取吗?有那么容易获取,网站早就不安全了。
热心网友
时间:2022-04-21 02:22
/// <summary>
/// 获取html代码
/// </summary>
/// <param name="url">网页地址</param>
/// <returns></returns>
public string GetHtml(string url)
{
string str = string.Empty;
try
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
WebResponse response = request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
str = streamReader.ReadToEnd();
streamReader.Close();
}
catch (Exception ex)
{ }
return str;
}
测试过 没问题 给分 有图有* 读取Mop的帖子
热心网友
时间:2022-04-21 04:30
三楼说的不错,最常用也是最先能想起的就是webclient了!
热心网友
时间:2022-04-21 06:55
WebBrowser不是.Net2.0内置的?