// WebClient
private string GetWebClient(string url)
{
string strHTML = "";
WebClient myWebClient = new WebClient();
Stream myStream = myWebClient.OpenRead(url);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
strHTML = sr.ReadToEnd();
myStream.Close();
return strHTML;
} // WebRequest
private string GetWebRequest(string url)
{
Uri uri = new Uri(url);
WebRequest myReq = WebRequest.Create(uri);
WebResponse result = myReq.GetResponse();
Stream receviceStream = result.GetResponseStream();
StreamReader readerOfStream = new StreamReader(receviceStream,System.Text.Encoding.GetEncoding("gb2312"));
string strHTML = readerOfStream.ReadToEnd();
readerOfStream.Close();
receviceStream.Close();
result.Close();
return strHTML;
} // HttpWebRequest
private string GetHttpWebRequest(string url)
{
try
{
Uri uri = new Uri(url);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
myReq.Accept = "*/*";
myReq.KeepAlive = true;
myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
Stream receviceStream = result.GetResponseStream();
StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("gb2312"));
string strHTML = readerOfStream.ReadToEnd();
readerOfStream.Close();
receviceStream.Close();
result.Close(); return strHTML;
}
catch (Exception ex)
{
throw new Exception("采集指定网址异常," + ex.Message);
}
}
// 获取网页源码,如果启用了gzip压缩后页面获取会产生乱码,采用此方法可解决gzip压缩而产生的乱码情况
private string GetHtmlCode(string url)
{
string htmlCode;
HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Timeout = ;
webRequest.Method = "GET";
webRequest.UserAgent = "Mozilla/4.0";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压 {
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
using (var zipStream =
new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
{
using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.Default))
{
htmlCode = sr.ReadToEnd();
}
}
}
}
else
{
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default))
{
htmlCode = sr.ReadToEnd();
}
}
} return htmlCode;
}

C#获取指定网页源码的几种方法的更多相关文章

  1. C#快速获取指定网页源码的几种方式,并通过字符串截取函数 或 正则 取指定内容(IP)

    //只获取网页源码开始到标题位目的进行测试 //第一种方式经过测试,稍微快点 string url = "http://www.ip.cn"; HttpWebRequest req ...

  2. c#下载网页源码的两种方法

    1.WebClient: System.Net.WebClient wc = new System.Net.WebClient(); Byte[] pageData = wc.DownloadData ...

  3. Python3.4 获取百度网页源码并保存在本地文件中

    最近学习python 版本 3.4 抓取网页源码并且保存在本地文件中 import urllib.request url='http://www.baidu.com' #上面的url一定要写明确,如果 ...

  4. 查看Chrome浏览器扩展程序源码的两种方法

    注意:仅在当前最新的版本 55.0.2883.87 m (64-bit)上测试有效 首先获取extensionId: chrome 打开扩展程序页面 chrome://extensions/ 比如我想 ...

  5. Make编译Ardupilot源码的两种方法

    编译环境准备 ​ Ardupilot源码下载和PX4 toolchain工具链下载 ​ (见https://www.cnblogs.com/BlogsOfLei/p/7707485.html) ​ 注 ...

  6. PHP 通过fsockopen函数获取远程网页源码

    <?php $fp = fsockopen("www.baidu.com", 80, &$errno, &$errstr, 10); if(!$fp) { e ...

  7. 高效获取网页源码COM

    目前获取网页源码有几种方法: 1.WebClient下载页面2.HttpWebRequest发请求获取3.com组件xmlhttp获取 三者比较:WebClient代码最少,效率最慢:xmlhttp代 ...

  8. html 网页源码解析:bs4中BeautifulSoup

    from bs4 import BeautifulSoup result=requests.request("get","http://www.baidu.com&quo ...

  9. c# 获取网页源码

    C#获取指定网页HTML原代码可使用 WebClient WebRequest HttpWebRequest 三种方式来实现. 当然也可使用webBrowse!在此就不研究webBrowse如何获取了 ...

随机推荐

  1. ubuntu设置ip和dns

      装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns.   下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需.   一.配置ip      ub ...

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9

    (1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...

  3. java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch Java之CountDownLatc ...

  4. HDU 3271-SNIBB(数位dp)

    题意:给一个数q, q=1时求给定区间,给定进制,各数位和等于m的数字的个数 q=2时求给定区间,给定进制,各数位和等于m的数字中的第k大的数字 分析:dp[i][sum][j],表示长度为i当前数位 ...

  5. 如何获取android app的Activity

    方法一 如有你有待测项目的源码,那么直接查看源码就好.如果没有,那么请联系有源码的同学,这是推荐方法.   方法二 直接把apk后缀改为zip格式,打开压缩包后再打开AndroidManifest.x ...

  6. wuzhicms短信API 实例调用

    1.接口调用 $sendsms = load_class('sms','sms'); echo $sendsms->send_sms('18911549611', '888888', 1); / ...

  7. 2014上海网络赛 HDU 5053 the Sum of Cube

    水 #include <stdio.h> #include <stdlib.h> #include<math.h> #include<iostream> ...

  8. HW7.9

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  9. HUOJ-10857 最大的面积 凸包+DP

    题目链接:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10857&courseid=55 比赛的时候 ...

  10. HDU-3487 Play with Chain Splay tee区间反转,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 对于一个数列有两种操作:1.CUT a b c,先取出a-b区间的数,然后把它们放在取出后的第c ...