【问题标题】:Get count of +1 of a webpage in c#在c#中获取网页的+1计数
【发布时间】:2012-05-25 09:16:13
【问题描述】:

我想要一种方法来获取网页 url 并返回它的 +1 计数。我搜索了谷歌,找到了this,方法是:

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

【问题讨论】:

    标签: c# .net c#-4.0 google-plus google-plus-one


    【解决方案1】:

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

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

    希望对你有帮助

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

    【讨论】:

    • 我有using System.Web;,但那里没有HttpUtility
    • 嗯,这就是它所在的地方,请参阅:msdn.microsoft.com/en-us/library/system.web.httputility.aspx
    • 什么是gp in line : string response = gp.Request(fetchUrl);?
    • 'gp' 是我为生成新代理而创建的一个类,'Request' 方法需要一个 url,然后在代理后面进行请求。您显然需要稍微修改此代码,但您需要的一切都在那里。
    • 好的。但在代码中if (match.Success) 总是返回false
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多