C#获取网页源代码的方法
作者:我心依旧 时间:2023-06-19 05:43:44
本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:
public string GetPageHTML(string url)
{
try
{
HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
wr.Method = "get";
wr.Accept = "*/*";
wr.Headers.Add("Accept-Language: zh-cn");
wr.Headers.Add("UA-CPU: x86");
wr.Headers.Add("Accept-Encoding: gzip, deflate");
wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Embedded Web Browser from: http://bsalsa.com/; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
wr.KeepAlive = true;
wr.ServicePoint.Expect100Continue = false;
wr.AllowAutoRedirect = false;
HttpWebResponse wre = wr.GetResponse() as HttpWebResponse;
StreamReader sreader = new StreamReader(wre.GetResponseStream(), Encoding.GetEncoding("GBK"));
string sHtml = sreader.ReadToEnd();
wre.Close();
return sHtml;
}
catch
{
return "";
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,网页,源代码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java.lang.OutOfMemoryError 错误整理及解决办法
2021-09-23 04:09:51
Java中Optional类及orElse方法详解
2022-07-12 03:21:36
在多线程中调用winform窗体控件的实现方法
2023-09-13 09:07:43
C++定义和初始化string对象实例详解
2022-08-05 06:03:00
JavaSE的逻辑控制你了解吗
2023-01-07 01:32:22
![](https://img.aspxhome.com/file/2023/0/79910_0s.png)
java实现网站微信扫码支付
2023-06-18 18:58:19
Spring Security角色继承分析
2022-02-03 03:39:12
springboot vue前后端接口测试树结点添加功能
2022-05-22 18:51:09
![](https://img.aspxhome.com/file/2023/4/119164_0s.png)
Java静态代理与动态代理案例详解
2021-12-09 19:55:44
![](https://img.aspxhome.com/file/2023/6/103826_0s.png)
一文告诉你为什么要重写hashCode()方法和equals()方法
2021-09-05 07:05:55
JPA多数据源分布式事务处理方案
2023-08-09 03:50:06
Spring Security使用Lambda DSL配置流程详解
2021-12-23 19:20:32
Spring Retry 重试实例详解
2021-07-15 22:43:26
![](https://img.aspxhome.com/file/2023/5/63495_0s.png)
Android入门之画图详解
2023-11-09 11:47:47
![](https://img.aspxhome.com/file/2023/4/93264_0s.jpg)
C#使用WebSocket实现聊天室功能
2022-08-10 09:29:33
![](https://img.aspxhome.com/file/2023/5/122615_0s.jpg)
详解SpringBoot定时任务说明
2022-08-11 05:03:11
![](https://img.aspxhome.com/file/2023/7/76547_0s.png)
WPF实现绘制扇形统计图的示例代码
2021-11-29 23:42:00
![](https://img.aspxhome.com/file/2023/8/91288_0s.png)
IntelliJ IDEA基于SpringBoot如何搭建SSM开发环境的步骤详解
2022-11-24 12:10:39
![](https://img.aspxhome.com/file/2023/9/79349_0s.png)
深入解析C#设计模式编程中对建造者模式的运用
2023-02-22 08:19:35
![](https://img.aspxhome.com/file/2023/7/122647_0s.png)
Android UI使用HTML布局方法实例
2023-04-07 09:14:11