我想要一种方法来获取网页网址并返回它的 +1 计数。我搜索了谷歌并找到了这个,方法是:
int GetPlusOnes(string url)
{
string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
string postData = @"[{""method"":""pos.plusones.get"",""id"":""p"",""params"":{""nolog"":true,""id"":""" + url + @""",""source"":""widget"",""userId"":""@viewer"",""groupId"":""@self""},""jsonrpc"":""2.0"",""key"":""p"",""apiVersion"":""v1""}]";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(googleApiUrl);
request.Method = "POST";
request.ContentType = "application/json-rpc";
request.ContentLength = postData.Length;
System.IO.Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string jsonString = readStream.ReadToEnd();
readStream.Close();
responseStream.Close();
response.Close();
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
int count = Int32.Parse(json[0]["result"]["metadata"]["globalCounts"]["count"].ToString().Replace(".0", ""));
return count;
}
但有一个错误
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
因为该Deserialize
方法有两个参数,而我不知道第二个参数。有人知道第二个参数是什么或知道另一种可以正常工作的方法吗?
更新 我也试过
public static Regex REGEX_GETURLCOUNT =
new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>(\d*)</div>");
public int GetPlusOnes(string url)
{
string fetchUrl =
"https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
HttpUtility.UrlEncode(url) + "&count=true";
WebClient wc=new WebClient();
string response = wc.DownloadString(fetchUrl);
Match match = REGEX_GETURLCOUNT.Match(response);
if (match.Success)
{
return int.Parse(match.Groups[1].Value);
}
return 0;
}
但match.Success
总是false
!