1

我想要一种方法来获取网页网址并返回它的 +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

4

1 回答 1

3

最近不得不做这样的事情,我用过:

 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;
        }

    }

希望这可以帮助

*编辑了我的自定义代理类,你可以剪切和粘贴它,它会为你工作。

于 2012-05-25T09:20:27.737 回答