C#获取google PageRank
2010-09-16 14:14 xchit 阅读(365) 评论(0) 编辑 收藏 举报using System;
using System.Net;
namespace BLL
{
/// <summary>
/// 下载信息url的html内容的类
/// </summary>
public class Net
{
public static string GetHtml(string url)
{
string html = "";
WebClient wc = new WebClient();
try { html = wc.DownloadString(url); }
catch { }
return html;
}
}
/// <summary>
/// 获取google pageran的类
/// </summary>
public class GooglePR
{
public static string GetPageRank(string strUrl)
{
string sURL = "http://toolbarqueries.google.com/search?client=navclient-auto&ch=" + GetPR(strUrl) + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + System.Web.HttpUtility.UrlEncode(strUrl);
string result = Net.GetHtml(sURL);
if (result != null)
{
result = result.Trim();
if (result.IndexOf(':') > 0)
{
string[] pr = result.Split(':');
if (pr.Length == 3) return pr[2].ToString();
}
}
return "0";
}
private static string GetPR(string url)
{
url = "info:" + url;
string ch = GoogleCH(str_asc(url)).ToString();
ch = "6" + ch;
return ch;
}
private static int[] str_asc(string str)
{
if (str == null || str == string.Empty) return null;
int[] result = new int[str.Length];
for (int i = 0; i < str.Length; i++) result[i] = (int)str[i];
return result;
}
private static long yiweitwo(long a, long b)
{
long z = 0x80000000;
if ((z & a) != 0)
{
a = (a >> 1);
a &= (~z);
a |= 0x40000000;
a = ((int)a >> (int)(b - 1));
}
else
{
a = ((int)a >> (int)b);
}
return a;
}
private static int[] yiwei(long a, long b, long c)
{
a -= b; a -= c; a ^= (yiweitwo(c, 13));
b -= c; b -= a; b ^= (a << 8);
c -= a; c -= b; c ^= (yiweitwo(b, 13));
a -= b; a -= c; a ^= (yiweitwo(c, 12));
b -= c; b -= a; b ^= (a << 16);
c -= a; c -= b; c ^= (yiweitwo(b, 5));
a -= b; a -= c; a ^= (yiweitwo(c, 3));
b -= c; b -= a; b ^= (a << 10);
c -= a; c -= b; c ^= (yiweitwo(b, 15));
return new int[] { (int)a, (int)b, (int)c };
}
private static int GoogleCH(int[] url)
{
int length = url.Length;
long a = 0x9E3779B9;
long b = 0x9E3779B9;
long c = 0xE6359A60;
int k = 0;
int len = length;
int[] mid;
while (len >= 12)
{
a += (url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
b += (url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
c += (url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
mid = yiwei(a, b, c);
a = mid[0]; b = mid[1]; c = mid[2];
k += 12;
len -= 12;
}
c += length;
switch (len)
{
case 11:
{
c += (url[k + 10] << 24);
c += (url[k + 9] << 16);
c += (url[k + 8] << 8);
b += (url[k + 7] << 24);
b += (url[k + 6] << 16);
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 10:
{
c += (url[k + 9] << 16);
c += (url[k + 8] << 8);
b += (url[k + 7] << 24);
b += (url[k + 6] << 16);
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 9:
{
c += (url[k + 8] << 8);
b += (url[k + 7] << 24);
b += (url[k + 6] << 16);
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 8:
{
b += (url[k + 7] << 24);
b += (url[k + 6] << 16);
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 7:
{
b += (url[k + 6] << 16);
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 6:
{
b += (url[k + 5] << 8);
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 5:
{
b += (url[k + 4]);
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 4:
{
a += (url[k + 3] << 24);
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 3:
{
a += (url[k + 2] << 16);
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 2:
{
a += (url[k + 1] << 8);
a += (url[k + 0]);
break;
}
case 1:
{
a += (url[k + 0]);
break;
}
}
mid = yiwei(a, b, c);
return mid[2];
}
}
}