分享如何获取做网站的流量分析统计
刚刚闲下来,把自己之前做网站流量统计的经验和大家分享一下
最开始也是无从下手,最后朋友建议用httpHandlers来做,结果等我把该弄的都弄好了,由于公司网站http://www.china-1718.com是租用的别人的服务器,不能够自己添加处理扩展名的应用程序。真是杯具了,接下来,我想只要能够记录下数据就行了,当时需要记录的数据就是来访者的IP、入口网址、来访网址、搜索引擎关键字等等,其他的都好办,就是在获取关键字这个阶段花了不少的时间,现在贴出代码来分享下,网上有很多,有的却达不到自己想要的效果,只能综合综合。。
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Stream
{
public class SearchKeyWord
{
private string[][] _Enginers = new string[][]
{
new string[]{"google","utf8","q"},
new string[]{"baidu","gb2312","wd"},
new string[]{"yahoo","utf8","p"},
new string[]{"yisou","utf8","search"},
new string[]{"live","utf8","q"},
new string[]{"tom","gb2312","word"},
new string[]{"163","gb2312","q"},
new string[]{"iask","gb2312","k"},
new string[]{"soso","gb2312","w"},
new string[]{"sogou","gb2312","query"},
new string[]{"zhongsou","gb2312","w"},
new string[]{"3721","gb2312","p"},
new string[]{"openfind","utf8","q"},
new string[]{"alltheweb","utf8","q"},
new string[]{"lycos","utf8","query"},
new string[]{"youdao","utf8","q"},
new string[]{"onseek","utf8","q"},
new string[]{"jike","utf8","q"}
};
//搜索引擎名称
private string _EngineName = "";
public string EngineName
{
get
{
return _EngineName;
}
}
//搜索引擎编码
private string _Coding = "utf8";
public string Coding
{
get
{
return _Coding;
}
}
//搜索引擎关键字查询参数名称
private string _RegexWord = "";
public string RegexWord
{
get
{
return _RegexWord;
}
}
private string _Regex = @"(";
//搜索引擎关键字
//建立搜索关键字正则表达式
public void EngineRegEx(string myString,out string strEngineName)
{
string strName = "";
for (int i = 0, j = _Enginers.Length; i < j; i++)
{
if (myString.Contains(_Enginers[i][0]))
{
_EngineName = _Enginers[i][0];
_Coding = _Enginers[i][1];
_RegexWord = _Enginers[i][2];
if (_EngineName == "baidu" && myString.Contains("word"))
{
_RegexWord = "word";
}
_Regex += _EngineName + @".+.*[?/&]" + _RegexWord + @"[=:])(?<key>[^&]*)";
strName= _EngineName;
break;
}
}
strEngineName = strName;
}
//得到搜索引擎关键字
public string SearchKey(string myString,out string strEngineName)
{
EngineRegEx(myString.ToLower(),out strEngineName);
if (_EngineName != "")
{
Regex myReg = new Regex(_Regex, RegexOptions.IgnoreCase);
Match matche = myReg.Match(myString);
myString = matche.Groups["key"].Value;
//替换特殊字符
myString = myString.Replace("+", " ").Replace("%20", " ").Replace("%5C", "\\").Replace("%2E", ".").Replace("%3F", "?").Replace("%23", "#").Replace("%26", "&").Replace("%3D", "=").Replace("%3A", ":").Replace("%28", "(").Replace("%29", ")").Replace("%7E", "~").Replace("%2F", "/").Replace("%09", " ").Replace("%2D", "-").ToString();
string strOld = myString.ToUpper();
string strUtf8 = "";
Encoding encoding = Encoding.UTF8;
//用utf-8进行解码
strUtf8 = System.Web.HttpUtility.UrlDecode(myString.ToUpper(), encoding);
//将已经解码的字符再次进行编码.
strUtf8 = System.Web.HttpUtility.UrlEncode(strUtf8, encoding).ToUpper();
//替换特殊字符
strUtf8 = strUtf8.Replace("+", " ").Replace("%20", " ").Replace("%5C", "\\").Replace("%2E", ".").Replace("%3F", "?").Replace("%23", "#").Replace("%26", "&").Replace("%3D", "=").Replace("%3A", ":").Replace("%28", "(").Replace("%29", ")").Replace("%7E", "~").Replace("%2F", "/").Replace("%09", " ").Replace("%2D", "-").ToString();
if (strUtf8 == strOld)
{
myString = System.Web.HttpUtility.UrlDecode(myString, encoding);
}
else
{
myString = System.Web.HttpUtility.UrlDecode(myString, Encoding.GetEncoding("gb2312"));
}
}
return myString;
}
}
}
对了还有个问题就是获取IP地址所在地,起初试了几个web服务,以为是免费的,结果还是得交钱钱,最后没办法,只能借用专门查询的网站获取网页源码来得到给出代码如下
/// <summary>
/// 获取HTML源码信息(Porschev)
/// </summary>
/// <param name="url">获取地址</param>
/// <returns>HTML源码</returns>
public string GetHtml(string url)
{
string str = "";
try
{
Uri uri = new Uri(url);
WebRequest wr = WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
str = sr.ReadToEnd();
}
catch (Exception e)
{
}
return str;
}