Reputation: 4657
I want a method to get a webpage url and return count of it's +1. I searched google and just find this and the method is:
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;
}
but there is an error in
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
because the Deserialize
method gets two arguments and I don't know the second argument.
does anybody know what is the second argument or know another methods that works fine?
update I also tried
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;
}
but match.Success
is always false
!
Upvotes: 1
Views: 1599
Reputation: 4468
Had to do something like this recently, i used:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int test = GetPlusOnes("http://www.allfancydress.com/"); //Returns 11 plus ones
Response.Write("Plus ones: " + test.ToString());
}
public static Regex REGEX_GETURLCOUNT =
new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>(\d*)</div>");
public static int GetPlusOnes(string url)
{
string fetchUrl =
"https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
HttpUtility.UrlEncode(url) + "&count=true";
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(fetchUrl);
string response = new StreamReader(request.GetResponse()
.GetResponseStream()).ReadToEnd();
Match match = REGEX_GETURLCOUNT.Match(response);
if (match.Success)
{
return int.Parse(match.Groups[1].Value);
}
return 0;
}
}
Hope this helps
*edited out my custom proxy class, you can just cut and paste this and it will work for you.
Upvotes: 2