教程:ASP.NET抓取网页内容的实现方法
优采云 发布时间: 2022-09-22 01:18教程:ASP.NET抓取网页内容的实现方法
/// 对于有BOM的网页非常有效,不管是什么编码,都能正确识别
///
///
网站地址"
/// 返回网页的源文件
公共静态字符串GetHtmlSource2(字符串url)
{
//处理内容
字符串 html = "";
HttpWebRequest 请求 = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*"; //接受任何文件
request.UserAgent = "Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.2;.NET CLR 1.1.432 2)"; //
request.AllowAutoRedirect = true;//是否允许302
//request.CookieContainer = new CookieContainer();//cookie容器,
request.Referer = url; //当前页面的引用
HttpWebResponse 响应 = (HttpWebResponse)request.GetResponse();
流流 = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
html = reader.ReadToEnd();
stream.Close();
返回html;
}
二、ASP.NET 使用 WebResponse 抓取网页内容
复制代码代码如下: public static string GetHttpData2(string Url)
{
字符串 sException = null;
字符串 sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
试试
{
oWebRps = oWebRqst.GetResponse();
}
捕获 (WebException e)
{
sException = e.Message.ToString();
}
捕获(异常 e)
{
sException = e.ToString();
}
终于
{
如果 (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
返回 sRslt;
}
希望本文对您的 C# 编程有所帮助。
内容分享:php如何禁止浏览器使用缓存页面
php禁止浏览器使用缓存页面的方法:1、设置这个页面的过期时间,代码为[header("Expires: Mon Jul 1970GMT")]; 2、告诉客户端浏览器不要使用缓存,代码是[header ( Pragma: ]。
php如何防止浏览器使用缓存页面:
在PHP中,可以很方便的使用下面的语句来禁止页面缓存,但是为了大家方便,比较难记和整理。
php代码如下:
代码如下:
这对某些页面很有用,例如订单下的单个信息和产品,以及清除购物车对应的产品数据。
我绝对不希望用户到最后一页已经生成订单,然后点击浏览器的返回按钮返回上一页。
然后在订单地址页面中添加:
代码如下:
header("Cache-Control:no-cache,must-revalidate,no-store"); //这个no-store加了之后,Firefox下有效
header("Pragma:no-cache");
header("Expires:-1");
这个页面不再缓存了,有一个页面判断购物车里的商品是空的后跳转到一个空的购物车,然后用户点击浏览器返回,返回后会直接进入购物车页面。
相关学习推荐:PHP编程从入门到精通