如何使用c#获取网址谷歌网页排名#

问题描述:

我使用下面的代码,但它是给出超时错误。虽然我的IP没有被阻止,我可以通过firefox addon searchstatus进行检查。我使用的下面的代码看起来像过时的,而不是体面的。所以我的问题是我怎么能得到URL的网页排名与C#4.0如何使用c#获取网址谷歌网页排名#

 private const UInt32 GOOGLE_MAGIC = 0xE6359A60; 

    private static void _mix(ref UInt32 a, ref UInt32 b, ref UInt32 c) 
    { 
     a -= b; a -= c; a ^= c >> 13; 
     b -= c; b -= a; b ^= a << 8; 
     c -= a; c -= b; c ^= b >> 13; 
     a -= b; a -= c; a ^= c >> 12; 
     b -= c; b -= a; b ^= a << 16; 
     c -= a; c -= b; c ^= b >> 5; 
     a -= b; a -= c; a ^= c >> 3; 
     b -= c; b -= a; b ^= a << 10; 
     c -= a; c -= b; c ^= b >> 15; 
    } 

    public static string GoogleCH(string url) 
    { 
     url = string.Format("info:{0}", url); 

     int length = url.Length; 

     UInt32 a, b; 
     UInt32 c = GOOGLE_MAGIC; 

     int k = 0; 
     int len = length; 

     a = b = 0x9E3779B9; 

     while (len >= 12) 
     { 
      a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24)); 
      b += (UInt32)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24)); 
      c += (UInt32)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24)); 
      _mix(ref a, ref b, ref c); 
      k += 12; 
      len -= 12; 
     } 
     c += (UInt32)length; 
     switch (len) /* all the case statements fall through */ 
     { 
      case 11: 
       c += (UInt32)(url[k + 10] << 24); 
       goto case 10; 
      case 10: 
       c += (UInt32)(url[k + 9] << 16); 
       goto case 9; 
      case 9: 
       c += (UInt32)(url[k + 8] << 8); 
       goto case 8; 
      /* the first byte of c is reserved for the length */ 
      case 8: 
       b += (UInt32)(url[k + 7] << 24); 
       goto case 7; 
      case 7: 
       b += (UInt32)(url[k + 6] << 16); 
       goto case 6; 
      case 6: 
       b += (UInt32)(url[k + 5] << 8); 
       goto case 5; 
      case 5: 
       b += (UInt32)(url[k + 4]); 
       goto case 4; 
      case 4: 
       a += (UInt32)(url[k + 3] << 24); 
       goto case 3; 
      case 3: 
       a += (UInt32)(url[k + 2] << 16); 
       goto case 2; 
      case 2: 
       a += (UInt32)(url[k + 1] << 8); 
       goto case 1; 
      case 1: 
       a += (UInt32)(url[k + 0]); 
       break; 
      default: 
       break; 
      /* case 0: nothing left to add */ 
     } 

     _mix(ref a, ref b, ref c); 

     return string.Format("6{0}", c); 
    } 

    public string getGooglePR(string url) 
    { 
     Uri myUri = new Uri(url); 
     string srUrl = myUri.AbsoluteUri.ToString(); 
     string checksum = GoogleCH(srUrl); 
     string query = string.Format("http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank&ch={0}&features=Rank&q=info:{1}", checksum, srUrl); 

     try 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query); 
      string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); 
      if (response.Length == 0) 
       return "01"; 
      else 
       return int.Parse(Regex.Match(response, "Rank_1:[0-9]:([0-9]+)").Groups[1].Value).ToString() + "0"; 
     } 
     catch (Exception E) 
     { 
      return E.Message.ToString(); 
     } 
    } 

C#4.0

通知:当有在网址中使用特殊字符,例如&

+0

您可能会考虑运行提琴手,看看确切的查询是由firefox发送的。 – NotMe 2012-01-17 21:36:33

+0

Chris Lively谢谢你的回答,但我发现了错误。错误是哈希函数。当网址中有特殊字符时,它就会失败。那么我该如何解决哈希问题 – MonsterMMORPG 2012-01-17 21:37:33

只是检查出现问题为特定网站的主机,而不是完整的网址

网站的URL主机有&?

+0

这会如何帮助。这里的目的是获得完整的网址pagerank – MonsterMMORPG 2012-02-17 09:26:45

+0

你好,我跑你发布的代码,并且它是抛出一个错误。算术错误。当我检查时,上面的代码没有获得PR,而是在搜索框中返回一个带有“info:domain”的网页作为查询,并且返回一个结果。该代码仍然有效,也许我可以帮你调试 – Smith 2012-02-17 13:38:12

+0

是的代码工程。我的成功率约为90% – MonsterMMORPG 2012-02-17 20:12:46