说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

ASP.NET(C#)获取Google PR值

HTML文档下载 WORD文档下载 PDF文档下载
许多网站都提供了查询Google PR值(PageRank)的功能,其实这些功能很好实现,但网上流传的ASP.NET版本对于校验码的计算存在错误致使无法正确运行,编者根据运行正确的PHP版本改编为C#版本,供大家编程时参考。
许多网站都提供了查询Google PR值(PageRank)的功能,其实这些功能依托.NET强大的能力,实现起来也是毫不费力,网上流传的ASP.NET(C#)代码对于校验码计算错误,无法得到正确的校验码,我根据正确的PHP代码改编为C#版本,代码如下:
using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Net;using System.IO;// 本代码是由"好饱"根据PHP版本改编,详情见我的专栏 http://www.okbase.net/home/haobaonamespace BLL{    public class Net    {        public static string GetHtml(string url)        {            string html = "";            WebClient wc = new WebClient();            try { html = wc.DownloadString(url); }            catch { }            return html;        }    }    public class GooglePR    {        public static Int64 StrToNum(string Str, Int64 Check, Int64 Magic)        {            Int64 Int32Unit = 4294967296;  // 2^32            int length = Str.Length;            char[] StrArray = Str.ToCharArray();            for (int i = 0; i < length; i++)            {                Check *= Magic; 	                                if (Check >= Int32Unit) {                    Check = (Check - Int32Unit * (Int64)(Check / Int32Unit));                    //if the check less than -2^31                    Check = (Check < -2147483648) ? (Check + Int32Unit) : Check;                }                Check += StrArray[i];            }            return Check;        }        //将URL进行哈希编码        public static Int64 HashURL(string String)        {            Int64 Check1 = StrToNum(String, 0x1505, 0x21);            Int64 Check2 = StrToNum(String, 0, 0x1003F);            Check1 >>= 2;            Check1 = ((Check1 >> 4) & 0x3FFFFC0) | (Check1 & 0x3F);            Check1 = ((Check1 >> 4) & 0x3FFC00) | (Check1 & 0x3FF);            Check1 = ((Check1 >> 4) & 0x3C000) | (Check1 & 0x3FFF);            Int64 T1 = ((((Check1 & 0x3C0) << 4) | (Check1 & 0x3C)) << 2) | (Check2 & 0xF0F);            Int64 T2 = ((((Check1 & 0xFFFFC000) << 4) | (Check1 & 0x3C00)) << 0xA) | (Check2 & 0xF0F0000);            return (T1 | T2);        }        //为哈希字符串生成校验码        public static string CheckHash(Int64 Hashnum)        {            int CheckByte = 0;            int Flag = 0;            string HashStr = string.Format("{0:D}", Hashnum);            int length = HashStr.Length;            Char[] HashStrArray = HashStr.ToCharArray();            for (int i = length - 1;  i >= 0;  i --) {                int Re = HashStrArray[i] - '0';                if (1 == (Flag % 2)) {                                  Re += Re;                    Re = (int)(Re / 10) + (Re % 10);                }                CheckByte += Re;                Flag++;	            }            CheckByte %= 10;            if (0 != CheckByte) {                CheckByte = 10 - CheckByte;                if (1 == (Flag % 2) ) {                    if (1 == (CheckByte % 2)) {                        CheckByte += 9;                    }                    CheckByte >>= 1;                }            }            return "7" + CheckByte.ToString() + HashStr.ToString();        }        // 得到ch校验码        public static string GetCH(string url)        {            return CheckHash(HashURL(url));         }        public static string GetPageRank(string strUrl)        {            string ch = GetCH(strUrl);            string sURL = "http://toolbarqueries.google.com/search?client=navclient-auto&ch=" + ch + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + strUrl;            string result = Net.GetHtml(sURL);            if (!String.IsNullOrEmpty(result))            {                result = result.Trim();                if (result.IndexOf(':') > 0)                {                    string[] pr = result.Split(':');                    if (pr.Length == 3)                        return pr[2].ToString();                }            }            return "0";        }    }}

调用格式举例:

 

string pr = GooglePR.GetPageRank("www.okbase.net");

 

备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘