statinfohandler.cs

来自「《精通ASP.NET2.0网络应用系统开发》书中的源码」· CS 代码 · 共 115 行

CS
115
字号

using System;
using System.Configuration;
using System.Web;
using System.Threading;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using webvisitStatistic.HitsHandler.DBLayer;

namespace webvisitStatistic.HitsHandler
{
	public class StatInfoHandler : IHttpHandler 
	{
		public void ProcessRequest(HttpContext context) 
		{
			// A file ending in .MyHello need not exist. This handler executes
			// whenever a file ending in .MyHello is requested.
			HttpRequest Request = context.Request;
			HttpResponse Response = context.Response;
			// if 被统计页面是无效页面
			String judge = IsValid(Request);
			if (!_isValid.Equals(judge)) {
				// 输出出错提示
				String str = String.Format(resCode2 ,GetBaseUrl() ,judge );
				Response.Write(str);
				Response.End();
			}

			
			try
			{
				String style = Request.QueryString["style"];
				if (style!="no")
				{
					String str = String.Format(resCode1 ,GetBaseUrl() ,GetBaseUrl() ,"网站访问统计实例");				
					Response.Write(str);
				}

				StatInfo statInfo = BuildStatInfo(Request);
				using ( DBLayer.IStatLogger statLogger = new DBLayer.SqlStatLogger())
				{
					if (statLogger.Init(System.Configuration.ConfigurationManager.AppSettings["ConnStrReporter"])) 
					{
						statLogger.Log(statInfo);
					}
				}
#if DEBUG
				// 记录这个消息到日志中去 
				Logger.Instance.LogStatInfo(statInfo);
#endif
			}catch(Exception ex)
			{
				Logger.Instance.Log("SIH " ,"ERRO" ,ex.Message);
			}finally{
				Response.End();
			}
		}
		public bool IsReusable 
		{
			// To enable pooling, return true here.
			// This keeps the handler in memory.
			get { return true; }  
		}

		// validate the client 
		// use webid isnt a good idea ,best use host + port
		protected String IsValid(HttpRequest request )
		{
			String webid = request.QueryString["webid"];
			String invalidwebid = "非法的webid,请到飞翔网站访问统计系统注册你的网站,获得一个合法的webid";
			if(webid==String.Empty)
				return "需要输入一个webid参数";
			if (0==int.Parse(webid)) {
				return invalidwebid;
			}
			return _isValid;
		}


		public StatInfo BuildStatInfo(HttpRequest Request)
		{
		
			StatInfo newStatInfo = new StatInfo();
			newStatInfo.WebId = int.Parse(Request.QueryString["webid"]);
			newStatInfo.ClientIP = Request.UserHostAddress;
			newStatInfo.URL	= Request.ServerVariables["HTTP_REFERER"];
		
			String[] userLangs = Request.UserLanguages;
			if(userLangs.Length>0)
				newStatInfo.UserLanguage = userLangs[0];
			else newStatInfo.UserLanguage = String.Empty;
			
			newStatInfo.RefUrl = Request.QueryString["referer"];
//			newStatInfo.PlatForm = Request.Browser.Platform;
//			newStatInfo.Browser = Request.Browser.Browser;
//			newStatInfo.MajorVersion = Request.Browser.MajorVersion;
//			newStatInfo.MinorVersion = Request.Browser.MinorVersion;
			newStatInfo.Client = new Client(Request.UserAgent,Request.QueryString["screenwidth"]);;
			
			return newStatInfo;
	
		}
		
		protected String GetBaseUrl()
		{
			return "http://localhost/webvisitStatistic";

		}
		protected const String _isValid = "true";
		private const String resCode2 = @"document.location.href='{0}/illegalweb.aspx?errmsg={1}'";
		private const String resCode1 = @"document.write(""<a href={0}/Default.aspx target=_blank><img src={1}/log.gif border=0 alt={2}></a>"")";
		private   Int64 _createTime = DateTime.Now.Ticks;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?