void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
//解决应用池回收问题
System.Threading.Thread.Sleep(5000);
string strUrl = "网站地址";
System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse _HttpWebResponse = (System.Net.HttpWebResponse)_HttpWebRequest.GetResponse();
System.IO.Stream _Stream = _HttpWebResponse.GetResponseStream();//得到回写的字节流
}
2.
下面给出一种有效的方法:
private const string DummyPageUrl = "http://localhost/Index.aspx";//可访问的页面
private const string DummyCacheItemKey = "Du";
// 注册一缓存条目在5分钟内到期,到期后触发的调事件
private void RegisterCacheEntry()
{
if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return;
HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null, DateTime.MaxValue,
TimeSpan.FromMinutes(5), CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(CacheItemRemovedCallback));
}
// 缓存项过期时程序模拟点击页面,阻止应用程序结束
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
HitPage();
}
// 模拟点击网站网页
private void HitPage()
{
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadData(DummyPageUrl);
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString() == DummyPageUrl)
{
RegisterCacheEntry();
}
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterCacheEntry();
}