calf喜欢用vb,近来突然想起做一个ie的自定义浏览器,同时想获取所浏览网站的Pr值 (PageRank)和Alexa排名,vb.net的代码实例实在难找,在网上众里寻它N回,终于让偶给找到了个比较好的代码,经过自己的小小修改终于可以用vb(vb.net 2008),获取pr和Alexa了
首先添加一个类,代码如下:
折叠ASP/Visual Basic Code复制内容到剪贴板
- Imports System
- Imports System.IO
- Public Class CheckSum
- Public Const GOOGLE_MAGIC As UInt32 = 3862272608
- Public Function zeroFill(ByVal a As UInt32, ByVal b As Integer) As UInt32
- Dim z As UInt32 = 2147483648
- If (Convert.ToBoolean(z And a)) Then
- a = (a >> 1)
- a = a And (Not z)
- a = a Or &H40000000
- a = (a >> (b - 1))
- Else
- a = (a >> b)
- End If
- Return a
- End Function
- Public Function mix(ByVal a As UInt32, ByVal b As UInt32, ByVal c As UInt32) As UInt32()
- a = ReduceUInt32(a, b)
- a = ReduceUInt32(a, c)
- 'a ^= Convert.ToUInt32(zeroFill(c, 13))
- 'a = PowerValue(a, Convert.ToUInt32(zeroFill(c, 13)))
- a = a Xor zeroFill(c, 13)
- b = ReduceUInt32(b, c)
- b = ReduceUInt32(b, a)
- 'b ^= Convert.ToUInt32(a << 8)
- b = b Xor (a << 8)
- c = ReduceUInt32(c, a)
- c = ReduceUInt32(c, b)
- 'c ^= Convert.ToUInt32(zeroFill(b, 13))
- c = c Xor zeroFill(b, 13)
- a = ReduceUInt32(a, b)
- a = ReduceUInt32(a, c)
- 'a ^= Convert.ToUInt32(zeroFill(c, 12))
- a = a Xor zeroFill(c, 12)
- b = ReduceUInt32(b, c)
- b = ReduceUInt32(b, a)
- 'b ^= Convert.ToUInt32(a << 16)
- b = b Xor (a << 16)
- c = ReduceUInt32(c, a)
- c = ReduceUInt32(c, b)
- 'c ^= Convert.ToUInt32(zeroFill(b, 5))
- c = c Xor zeroFill(b, 5)
- a = ReduceUInt32(a, b)
- a = ReduceUInt32(a, c)
- 'a ^= Convert.ToUInt32(zeroFill(c, 3))
- a = a Xor zeroFill(c, 3)
- b = ReduceUInt32(b, c)
- b = ReduceUInt32(b, a)
- 'b ^= Convert.ToUInt32(a << 10)
- b = b Xor (a << 10)
- c = ReduceUInt32(c, a)
- c = ReduceUInt32(c, b)
- 'c ^= Convert.ToUInt32(zeroFill(b, 15))
- c = c Xor zeroFill(b, 15)
- Dim returnArray() As UInt32 = {a, b, c}
- Return returnArray
- End Function
- Public Function GoogleCH(ByVal url As UInt32(), ByVal length As UInt32, ByVal init As UInt32) As UInt32
- If length = 0 Then
- length = Convert.ToUInt32(url.Length - 1)
- End If
- Dim a As UInt32 = 2654435769
- Dim b As UInt32 = 2654435769
- Dim c As UInt32 = init
- Dim k As Integer = 0
- Dim len As UInt32 = length
- Dim m_mix(3) As UInt32
- While len >= 12
- 'a += Convert.ToUInt32(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24)))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4) + (url(k + 5) << 8) + (url(k + 6) << 16) + (url(k + 7) << 24)))
- c = AddUInt32(c, Convert.ToUInt32(url(k + 8) + (url(k + 9) << 8) + (url(k + 10) << 16) + (url(k + 11) << 24)))
- m_mix = mix(a, b, c)
- a = m_mix(0)
- b = m_mix(1)
- c = m_mix(2)
- k += 12
- len -= 12
- End While
- c += length
- Select Case len ' all the case statements fall through
- Case 11
- c = AddUInt32(c, Convert.ToUInt32(url(k + 10) << 24))
- c = AddUInt32(c, Convert.ToUInt32(url(k + 9) << 16))
- c = AddUInt32(c, Convert.ToUInt32(url(k + 8) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 7) << 24))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 6) << 16))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 10
- c = AddUInt32(c, Convert.ToUInt32(url(k + 9) << 16))
- c = AddUInt32(c, Convert.ToUInt32(url(k + 8) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 7) << 24))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 6) << 16))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 9
- c = AddUInt32(c, Convert.ToUInt32(url(k + 8) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 7) << 24))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 6) << 16))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- ' the first byte of c is reserved for the length
- Case 8
- b = AddUInt32(b, Convert.ToUInt32(url(k + 7) << 24))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 6) << 16))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 7
- b = AddUInt32(b, Convert.ToUInt32(url(k + 6) << 16))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 6
- b = AddUInt32(b, Convert.ToUInt32(url(k + 5) << 8))
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 5
- b = AddUInt32(b, Convert.ToUInt32(url(k + 4)))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 4
- a = AddUInt32(a, Convert.ToUInt32(url(k + 3) << 24))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 3
- a = AddUInt32(a, Convert.ToUInt32(url(k + 2) << 16))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 2
- a = AddUInt32(a, Convert.ToUInt32(url(k + 1) << 8))
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- Case 1
- a = AddUInt32(a, Convert.ToUInt32(url(k + 0)))
- ' case 0: nothing left to add
- End Select
- m_mix = mix(a, b, c)
- '-------------------------------------------- report the result
- Return m_mix(2)
- End Function
- Public Function GoogleCH(ByVal url As String, ByVal length As UInt32) As UInt32
- Dim m_urluint(url.Length) As UInt32
- Dim i As Integer
- For i = 0 To url.Length - 1
- m_urluint(i) = Convert.ToUInt32(url(i))
- Next
- Return GoogleCH(m_urluint, length, GOOGLE_MAGIC)
- End Function
- Public Function GoogleCH(ByVal sURL As String) As UInt32
- Return GoogleCH(sURL, 0)
- End Function
- Public Function GoogleCH(ByVal url() As UInt32, ByVal length As UInt32) As UInt32
- Return GoogleCH(url, length, GOOGLE_MAGIC)
- End Function
- Public Function c32to8bit(ByVal arr32() As UInt32) As UInt32()
- Dim arr8((arr32.GetLength(0) - 1) * 4 + 3) As UInt32
- Dim i As Integer
- For i = 0 To arr32.GetLength(0) - 1
- Dim bitOrder As Integer
- For bitOrder = i * 4 To i * 4 + 3
- arr8(bitOrder) = arr32(i) And 255
- arr32(i) = zeroFill(arr32(i), 8)
- Next
- Next
- Return arr8
- End Function
- Public Function AddUInt32(ByVal a As UInt32, ByVal b As UInt32) As UInt32
- Dim resultValue As UInt32
- Dim a64 As UInt64 = Convert.ToUInt64(a)
- Dim b64 As UInt64 = Convert.ToUInt64(b)
- Dim result64 As Int64 = a64 + b64
- Dim sStr As String = Convert.ToString(result64, 2)
- Dim sResult As String
- If sStr.Length > 32 Then
- sResult = sStr.Substring(sStr.Length - 32)
- Else
- sResult = sStr
- End If
- resultValue = Convert.ToUInt32(sResult, 2)
- Return resultValue
- End Function
- Public Function ReduceUInt32(ByVal a As UInt32, ByVal b As UInt32) As UInt32
- Dim resultValue As UInt32
- Dim aTemp As Int64 = a
- Dim bTemp As Int64 = b
- Dim resultTemp As Int64 = aTemp - bTemp
- Dim resultBinStr As String = Convert.ToString(resultTemp, 2)
- Dim sResult As String
- If resultBinStr.Length > 32 Then
- sResult = resultBinStr.Substring(resultBinStr.Length - 32)
- Else
- sResult = resultBinStr
- End If
- resultValue = Convert.ToUInt32(sResult, 2)
- Return resultValue
- End Function
- Public Function GetUInt32Value(ByVal a As Int64) As UInt32
- Dim tempValue As Int64 = a
- Dim resultBinStr As String = Convert.ToString(tempValue, 2)
- Dim sResult As String
- If resultBinStr.Length > 32 Then
- sResult = resultBinStr.Substring(resultBinStr.Length - 32)
- Else
- sResult = resultBinStr
- End If
- tempValue = Convert.ToUInt32(sResult, 2)
- Return tempValue
- End Function
- Public Function Mul(ByVal x As UInt32, ByVal y As UInt32)
- Dim r As UInt32 = 0
- Dim i As Int32 = 0
- For i = 32 To 0 Step -1
- r = r << 1
- If x >> i And 1 Then
- Dim r64 As Int64 = r
- Dim y64 As Int64 = y
- Dim tempResult As Int64 = r64 + y64
- r = GetUInt32Value(tempResult)
- End If
- Next
- Return r
- End Function
- Public Function PowerValue(ByVal a As UInt32, ByVal b As UInt32)
- Dim resultValue As UInt32 = a
- Dim i As Integer
- For i = 1 To b - 1
- resultValue = Mul(resultValue, a)
- Next
- Return resultValue
- End Function
- Public Function DEC_to_BIN(ByVal Dec As Int64) As String
- DEC_to_BIN = ""
- If Dec > 0 Then
- Do While Dec > 0
- DEC_to_BIN = Math.Abs(Dec Mod 2) & DEC_to_BIN
- Dec = Dec \ 2
- Loop
- Else
- End If
- End Function
- Public Function BIN_to_DEC(ByVal Bin As String) As UInt32
- Dim i As UInt32
- For i = 1 To Len(Bin)
- BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1))
- Next i
- End Function
- 'new,ToolBar edition>>=2.0.114
- Public Function CalculateChecksum(ByVal sURL As String) As String
- Dim ch As UInt32 = GoogleCH("info:" + sURL)
- ch = (((ch \ 7) << 2) Or ((Convert.ToUInt32(ch Mod 13)) And 7))
- Dim prbuf(20) As UInt32
- prbuf(0) = ch
- Dim i As Integer
- For i = 1 To 20 - 1
- prbuf(i) = prbuf(i - 1) - 9
- Next
- ch = GoogleCH(c32to8bit(prbuf), 80)
- Return String.Format("6{0}", ch)
- End Function
- 'old,ToolBar edition<2.0.114
- Public Function CalculateChecksumOld(ByVal sURL As String) As String
- Dim ch As UInt32 = GoogleCH("info:" + sURL)
- Dim CalculateChecksum As String = "6" + Convert.ToString((ch))
- Return CalculateChecksum
- End Function
- End Class
再加一个函数(可以在另一个模块里加),代码如下:
折叠ASP/Visual Basic Code复制内容到剪贴板
- '==============================
- ' if return value = -1, it means there is an error
- Public Function GetPr(ByVal sUrl As String) As Integer
- Dim iPageRank As Integer = -1
- Dim responseFromServer As String = ""
- 'Dim cs As GetPageRank = New GetPageRank()
- Dim cs As CheckSum = New CheckSum()
- 'Dim cs As PageRankCrack.PageRank = New PageRank()
- Dim s As String = cs.CalculateChecksum(sUrl)
- Try
- Dim HttpWReq As Net.HttpWebRequest
- ' HttpWReq = Net.WebRequest.Create("http://www.google.cn/search?client=navclient-auto&features=Rank:&q=info:" + sUrl + "&ch=" + s)
- HttpWReq = Net.WebRequest.Create("http://66.249.89.149/search?client=navclient-auto&ch=6138193407&ie=UTF-8&oe=UTF-8&features=Rank:FVN&q=info:" + sUrl + "&ch=" + s)
- 'http://66.249.89.149/search?client=navclient-auto&ch=6138193407&ie=UTF-8&oe=UTF-8&features=Rank:FVN&q=info:
- HttpWReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)"
- HttpWReq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*"
- Dim HttpWResp As Net.HttpWebResponse = HttpWReq.GetResponse()
- Dim dataStream As Stream = HttpWResp.GetResponseStream()
- Dim reader As StreamReader
- reader = New StreamReader(dataStream, System.Text.Encoding.GetEncoding("UTF-8"))
- responseFromServer = reader.ReadToEnd()
- Catch ex As Exception
- ' do nothing
- End Try
- Dim sp() As String = Split(responseFromServer, ":")
- If sp.Length = 3 Then
- iPageRank = Convert.ToInt32(sp(2).ToString())
- End If
- Return iPageRank
- 'If iPageRank <> -1 Then
- ' lblResult.Text = "success! Page Rank = " + iPageRank.ToString()
- 'Else
- ' lblResult.Text = "error!"
- 'End If
- End Function
最后调用就可以了(以下为例子,具体自己替换)
ASP/Visual Basic Code复制内容到剪贴板
- Try
- Dim queryUrl As String = searchE(txtUrl.Text, "^.+\.(com.cn|com|net.cn|net|org.cn|org|gov.cn|gov|cn|mobi|me|info|name|biz|cc|tv|asia|hk|网络|公司|中国)\/") '用正则从网址中获取主域名
- lblPr.Text = "Pr: " & GetPr(queryUrl) 'dim pr as string=GetPr("spersky.com")
- Dim strConfig As String = "http://data.alexa.com/data?cli=10&dat=snba&url=" & queryUrl 'queryUrl 代表网址如spersky.com
- Dim reader As New Xml.XmlTextReader(strConfig)
- Dim alexaInfo As String = ""
- While reader.Read
- If reader.Name = "POPULARITY" Then
- alexaInfo = reader.GetAttribute("TEXT") 'strConfig的值复制到地址栏,看结果,就会明白为什么要这样
- End If
- End While
- reader.Close()
- If alexaInfo = "" Then
- lblAlexa.Text = "Alexa: " & "Nameless"
- Else
- lblAlexa.Text = "Alexa: " & alexaInfo
- End If
- Catch ex As Exception
- MsgBox(ex)
- Exit Sub
- End Try