获取GZip加密的网页源码
2016-03-07 17:19
288 查看
public static string GetPage(string url, Encoding encoding)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.KeepAlive = true;
request.Timeout = 20000;
request.ContentType = "text/html; charset=UTF-8";
// request.UserAgent = "<a href=\"https://www.baidu.com/s?wd=Mozilla&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBuWnzrHN-PvDLuj0sPHmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnvnWczPHDzP1Dsrj0LrHTz\"
target=\"_blank\" class=\"baidu-highlight\">Mozilla</a>/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2593.0 Safari/537.36";
request.Accept = "*/*";
request.KeepAlive = true;
request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (Stream streamResponse = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(streamResponse, encoding);
return streamReader.ReadToEnd();
}
}
}
catch(Exception ex)
{
return string.Empty;
}
}
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.KeepAlive = true;
request.Timeout = 20000;
request.ContentType = "text/html; charset=UTF-8";
// request.UserAgent = "<a href=\"https://www.baidu.com/s?wd=Mozilla&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBuWnzrHN-PvDLuj0sPHmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnvnWczPHDzP1Dsrj0LrHTz\"
target=\"_blank\" class=\"baidu-highlight\">Mozilla</a>/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2593.0 Safari/537.36";
request.Accept = "*/*";
request.KeepAlive = true;
request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (Stream streamResponse = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(streamResponse, encoding);
return streamReader.ReadToEnd();
}
}
}
catch(Exception ex)
{
return string.Empty;
}
}
相关文章推荐
- 贪心:SPOJ Backup Files
- 关于类的方法调用
- RXJaava
- django入门教程(一)
- SuperMap iMobile for Android定位实现
- 抽象工厂模式
- 自动布局第三方Neon的基础使用
- TabLayout用法,android顶部导航栏,android底部导航栏
- 122.View the Exhibit for the structure of the STUDENT and FACULTY tables.
- linux远程下载文件 的两种方法之 ftp命令和scp命令
- golang martini 源码阅读笔记之inject
- spring mvc 集成memcached缓存服务器
- IE 下JS上传文件时出现“拒绝访问”的解决方案
- NSSet类型 以及与NSArray区别
- Installing the Eclipse Plugin
- 概率论与数理统计习题: 某县城有1万辆自行车,其牌照号码从00001到10000,求在该县城偶遇一辆自行车的牌照号码中有数字8的概率.
- 1000==1000? 无花括号的for循环,*.05x3=?
- 深入剖析Java中的装箱和拆箱
- thinking between bindService&startService
- batch语言卸载msi安装程序