获取网站来源,以及搜索引擎来源的关键字,并提取类:C#版本。
主类:UrlOpers
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.Text.RegularExpressions;
namespace HC.Common
{
public class UrlOpers
{
/// <summary>
/// 获取url参数和值,参数为nvc.Keys[i],对应的值为nvc[i]
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static NameValueCollection ParseUrl(string url)
{
if (url == null)
return null;
NameValueCollection nvc = new NameValueCollection();
int questionMarkIndex = url.IndexOf('?');
if (questionMarkIndex == -1)
{
return null;
}
if (questionMarkIndex == url.Length - 1)
return null;
string ps = url.Substring(questionMarkIndex + 1);
// 开始分析参数对
Regex re = new Regex(@"(^|&)?(/w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
MatchCollection mc = re.Matches(ps);
foreach (Match m in mc)
{
nvc.Add(m.Result("$2"), m.Result("$3"));
}
return nvc;
}
public static void SetLaiYuan()
{
string LaiyuanUrl = "";//来源url
string LaiyuanHost = "";//来源域名
string LaiyuanQuery = "";//查询字符串
string CurrentPage = "";//目标页
CurrentPage = configHelper.GetConfigString("Domain") + HttpContext.Current.Request.RawUrl.ToString();
if (HttpContext.Current.Request.UrlReferrer != null)
{
LaiyuanHost = HttpContext.Current.Request.UrlReferrer.Host.ToString().ToLower();
LaiyuanQuery = HttpContext.Current.Request.UrlReferrer.PathAndQuery.ToString();
LaiyuanUrl = LaiyuanHost + LaiyuanQuery;
string code = getCode(LaiyuanUrl);
string keyName = LaiyuanKeyName(LaiyuanUrl, code);
if (LaiyuanHost.IndexOf(configHelper.GetConfigString("Domain")) == -1) //如果来自其它网站的链接,则更新laiyuan和laiyuan2
{
Cookies.SetCookie("laiyuanKeyName",keyName, false);
Cookies.SetCookie("laiyuan", LaiyuanUrl, false);
Cookies.SetCookie("laiyuan2", CurrentPage, false);
}
}
}
/// <summary>
/// 读取用户来源,如果laiyuan为空则返回默认值
/// </summary>
/// <returns></returns>
public static string GetLaiYuan()
{
if (Cookies.GetCookie("laiyuan") != string.Empty)
{
return Cookies.GetCookie("laiyuan");
}
else
{
if (Cookies.GetCookie("laiyuan2") != string.Empty)
return (configHelper.GetConfigString("Domain") + "," + Cookies.GetCookie("laiyuan2"));
else
return (configHelper.GetConfigString("Domain"));
}
}
/// <summary>
/// 读取url关键字
/// </summary>
/// <returns></returns>
public static string getLaiYuanKeyName()
{
if (Cookies.GetCookie("laiyuanKeyName") != string.Empty)
{
return Cookies.GetCookie("laiyuanKeyName");
}
else
return "";
}
/// <summary>
/// 得到url编码
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string getCode(string url)
{
Uri u = new Uri(url);
string host = u.Host.ToString().ToLower();
string query = u.PathAndQuery.ToString();
url = "http://" + host + query;
NameValueCollection nvc = ParseUrl(url);
string bianma = "UTF-8";
string gb2312 = "www.baidu.com,www1.baidu.com,www.baidu.com.,www.baidu.com.hk,202.108.22.5,119.75.213.61,zhidao.baidu.com,news.baidu.com,cache.baidu.com,tieba.baidu.com,cpro.baidu.com,114search.118114.cn,www.soso.com,web.gougou.com,www.sogou.com,hap1.ucweb.com.cn";
if (gb2312.IndexOf(host) > -1)
bianma = "GB2312";
if (nvc != null)
{
#region 处理hap1.ucweb.com.cn,如:hap1.ucweb.com.cn/baidu_groups/web/http/m7.baidu.com/coop/s?pn=6&word=%E5%B9%BF%E6%B1%89%E6%9C%89%E6%B2%A1%E8%8B%B1%E8%AF%AD%E8%BF%87%E7%BA%A7%E8%80%83%E8%AF%95%E7%9A%84%E5%9F%B9%E8%AE%AD%E7%8F%AD&rn=6&from=2001f。默认的是gb2312,如果pn!="" and tn="" 那么编码就是utf-8
if (host == "hap1.ucweb.com.cn")
{
bool mark1 = false;
bool mark2 = false;
for (int i = 0; i < nvc.Count; i++)
{
if (nvc.Keys[i].ToString().Trim() == "pn" && nvc[i] != "")
{
mark1 = true;
}
if (nvc.Keys[i].ToString().Trim() == "tn" && nvc[i] != "")
{
mark2 = true;
}
}
if (mark1 && !mark2)
bianma = "UTF-8";
}
#endregion
else
{
for (int i = 0; i < nvc.Count; i++)
{
if (nvc.Keys[i].ToString().Trim() == "ie")
{
if (nvc[i] != "")
bianma = nvc[i];
}
}
}
}
return bianma;
}
/// <summary>
/// 得到来源的关键字,主要用于搜索引擎
/// </summary>
/// <param name="url"></param>
/// <param name="code"></param>
/// <returns></returns>
public static string LaiyuanKeyName(string url, string code)
{
string keyName = "";
if (url == null)
return keyName;
Uri u = new Uri(url);
string host = u.Host.ToString().ToLower();
string query = u.PathAndQuery.ToString();
url = "http://" + host + System.Web.HttpUtility.UrlDecode(query, System.Text.UnicodeEncoding.GetEncoding(code));
NameValueCollection nvc = ParseUrl(url);
string strkeyName = ",wd,word,kw,q,search,url,query,p,w,";
if (nvc != null)
{
string mark = "";
for (int i = 0; i < nvc.Count; i++)
{
if (strkeyName.IndexOf("," + nvc.Keys[i].ToString().Trim() + ",") > -1 && mark != nvc[i])
{
keyName += nvc[i];
mark = nvc[i];
}
}
}
return keyName;
}
}
}
cookies类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Security.Cryptography;
namespace HC.Common
{
public class Cookies
{
/// <summary>
/// 设置Cookie值
/// </summary>
/// <param name="ckName">Cookie名称</param>
/// <param name="ckValue">Cookie值</param>
/// <param name="ckday">Cookie有效期,单位天</param>
public static void SetCookie(string ckName, string ckValue,bool bhash)
{
SetCookie(ckName, ckValue, 1,bhash);
}
public static void SetCookie(string ckName, string ckValue, int ckday,bool bhash)
{
HttpCookie MyCookie = new HttpCookie(ckName, HttpContext.Current.Server.UrlEncode(ckValue));
MyCookie.Domain = configHelper.GetConfigString("Domain");
MyCookie.Expires = DateTime.Now.AddDays(ckday);
HttpContext.Current.Response.Cookies.Add(MyCookie);
//写入校验码,来源不需要校验
if (bhash)
{
HttpCookie MyCookieHash = new HttpCookie(ckName + "_hash", TripleDESCrypto(ckValue));
MyCookieHash.Domain = configHelper.GetConfigString("Domain");
MyCookieHash.Expires = DateTime.Now.AddDays(ckday);
HttpContext.Current.Response.Cookies.Add(MyCookieHash);
}
}
/// <summary>
/// 根据Cookie名称得到相应的值
/// </summary>
/// <param name="ckName">Cookie名称</param>
/// <returns>返回相应的值</returns>
public static string GetCookie(string ckName,bool bhash)
{
return GetCookie(ckName, string.Empty,bhash);
}
public static string GetCookie(string ckName, string defValue,bool bhash)
{
string ckValue = defValue;
string ckValue_hash = TripleDESCrypto(defValue);
if (ckName.Length > 0)
{
HttpCookie ht = new HttpCookie(ckName);
ht.Domain = configHelper.GetConfigString("Domain");
ht = HttpContext.Current.Request.Cookies[ckName];
if (ht != null)
ckValue = HttpContext.Current.Request.Cookies[ckName].Value;
ckValue = HttpContext.Current.Server.UrlDecode(ckValue);
//读取校验码,来源不需要校验
if (bhash)
{
HttpCookie ht_hash = new HttpCookie(ckName + "_hash");
ht_hash.Domain = configHelper.GetConfigString("Domain");
ht_hash = HttpContext.Current.Request.Cookies[ckName + "_hash"];
if (ht_hash != null)
ckValue_hash = HttpContext.Current.Request.Cookies[ckName + "_hash"].Value;
if (TripleDESCryptoDe(ckValue_hash).TrimEnd() != ckValue && ckName != "laiyuan")
ckValue = string.Empty;
}
}
return ckValue;
}
/// <summary>
/// 使用TripleDES加密
/// </summary>
/// <param name="?">需要加密的字符串</param>
/// <returns></returns>
public static string TripleDESCrypto(string str)
{
return TripleDESCrypto(str, @"]#gfV'8P""3]@xR:Z");
}
/// <summary>
/// 使用TripleDES加密
/// </summary>
/// <param name="str">需要加密的字符串</param>
/// <param name="key">16或24位密钥</param>
/// <returns></returns>
public static string TripleDESCrypto(string str, string key)
{
if (String.IsNullOrEmpty(str) || String.IsNullOrEmpty(key))
return string.Empty;
byte[] data = UnicodeEncoding.Unicode.GetBytes(str);
byte[] keys = ASCIIEncoding.ASCII.GetBytes(key);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = keys;
des.Mode = CipherMode.ECB;//设置运算模式
ICryptoTransform cryp = des.CreateEncryptor();//加密
return Convert.ToBase64String(cryp.TransformFinalBlock(data, 0, data.Length));
}
/// <summary>
/// 使用TripleDES解密
/// </summary>
/// <param name="str">需要解密的字符串</param>
/// <returns></returns>
public static string TripleDESCryptoDe(string str)
{
return TripleDESCryptoDe(str, @"]#gfV'8P""3]@xR:Z");
}
/// <summary>
/// 使用TripleDES解密
/// </summary>
/// <param name="str">需要解密的字符串</param>
/// <param name="key">16或24位密钥</param>
/// <returns></returns>
public static string TripleDESCryptoDe(string str, string key)
{
if (String.IsNullOrEmpty(str) || String.IsNullOrEmpty(key))
return string.Empty;
byte[] data = Convert.FromBase64String(str);
byte[] keys = ASCIIEncoding.ASCII.GetBytes(key);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = keys;
des.Mode = CipherMode.ECB;//设置运算模式
des.Padding = PaddingMode.PKCS7;
ICryptoTransform cryp = des.CreateDecryptor();//解密
return UnicodeEncoding.Unicode.GetString(cryp.TransformFinalBlock(data, 0, data.Length));
}
}
}
configHelper类
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace HC.Common
{
public sealed class configHelper
{
/// <summary>
/// 得到AppSettings中的配置字符串信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConfigString(string key)
{
string CacheKey = "AppSettings-" + key;
object objModel = DataCache.GetCache(CacheKey);
if (objModel == null)
{
try
{
objModel = ConfigurationManager.AppSettings[key];
if (objModel != null)
{
DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
}
}
catch
{ }
}
return objModel.ToString();
}
/// <summary>
/// 得到AppSettings中的配置Bool信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
bool result = false;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = bool.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置Decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static decimal GetConfigDecimal(string key)
{
decimal result = 0;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = decimal.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置int信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetConfigInt(string key)
{
int result = 0;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = int.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
}
}
DataCache类
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace HC.Common
{
public class DataCache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
}
}