Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares

ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares

The following displays how to get total comments, comment count, total likes, like count, total shares, share count, total count, click count, comment box count of one or more URL that are shared or integrated with Facebook by using ASP.NET (C#).

This article includes getting Facebook Share Statistics result (in XML format), parsing the out put string and displaying on the browser.

At the end, I also provide the full source code and a demonstration link for you to play with.

ASP.NET (C#) Get Facebook Share Statistics – Total Comments, Total Likes, Total Shares

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
 
public partial class aspx_get_total_comments_total_likes_total_shares_of_facebook_url : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (txtURLs.Text == "")
                txtURLs.Text = "http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/";
        }
    }
 
    public string get_web_content(string url)
    {
        Uri uri = new Uri(url);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Get;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string output = reader.ReadToEnd();
        response.Close();
 
        return output;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string request_url = txtURLs.Text.Trim();
        request_url = Server.UrlEncode(request_url);
        request_url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" + request_url;
 
        lblRequestURL.Text = request_url;
        hplRawResult.NavigateUrl = request_url;
 
        string Facebook_raw_data = get_web_content(request_url);
 
        XmlDocument dom = new XmlDocument();
        dom.LoadXml(Facebook_raw_data);
 
        XmlNodeList root = dom.GetElementsByTagName("link_stat");
 
        string result = "";
 
        foreach (XmlNode node in root)
        {
            XmlElement companyElement = (XmlElement)node;
 
            result += "<b>URL: </b>" + companyElement.GetElementsByTagName("url")[0].InnerText + "<br>";
            result += "<b>share_count: </b> " + companyElement.GetElementsByTagName("share_count")[0].InnerText + "<br>";
            result += "<b>like_count: </b>: " + companyElement.GetElementsByTagName("like_count")[0].InnerText + "<br>";
            result += "<b>comment_count: </b>: " + companyElement.GetElementsByTagName("comment_count")[0].InnerText + "<br>";
            result += "<b>total_count: </b>: " + companyElement.GetElementsByTagName("total_count")[0].InnerText + "<br>";
            result += "<b>click_count: </b>: " + companyElement.GetElementsByTagName("click_count")[0].InnerText + "<br>";
            result += "<b>commentsbox_count: </b>: " + companyElement.GetElementsByTagName("commentsbox_count")[0].InnerText + "<br>";
            result += "------------------------------------------<br>";
        }
 
        ltrResult.Text = result;
    }
}

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Xml; public partial class aspx_get_total_comments_total_likes_total_shares_of_facebook_url : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (txtURLs.Text == "") txtURLs.Text = "http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/"; } } public string get_web_content(string url) { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = WebRequestMethods.Http.Get; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string output = reader.ReadToEnd(); response.Close(); return output; } protected void btnSubmit_Click(object sender, EventArgs e) { string request_url = txtURLs.Text.Trim(); request_url = Server.UrlEncode(request_url); request_url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" + request_url; lblRequestURL.Text = request_url; hplRawResult.NavigateUrl = request_url; string Facebook_raw_data = get_web_content(request_url); XmlDocument dom = new XmlDocument(); dom.LoadXml(Facebook_raw_data); XmlNodeList root = dom.GetElementsByTagName("link_stat"); string result = ""; foreach (XmlNode node in root) { XmlElement companyElement = (XmlElement)node; result += "<b>URL: </b>" + companyElement.GetElementsByTagName("url")[0].InnerText + "<br>"; result += "<b>share_count: </b> " + companyElement.GetElementsByTagName("share_count")[0].InnerText + "<br>"; result += "<b>like_count: </b>: " + companyElement.GetElementsByTagName("like_count")[0].InnerText + "<br>"; result += "<b>comment_count: </b>: " + companyElement.GetElementsByTagName("comment_count")[0].InnerText + "<br>"; result += "<b>total_count: </b>: " + companyElement.GetElementsByTagName("total_count")[0].InnerText + "<br>"; result += "<b>click_count: </b>: " + companyElement.GetElementsByTagName("click_count")[0].InnerText + "<br>"; result += "<b>commentsbox_count: </b>: " + companyElement.GetElementsByTagName("commentsbox_count")[0].InnerText + "<br>"; result += "------------------------------------------<br>"; } ltrResult.Text = result; } }

In the CodeFile above:

  • 1. The get_web_content() function simply use ASP.NET Web Request to get the Facebook Share Statistics output in XML format from Facebook getStats link.
  • 2. When people click Submit button, it will encode the input URLs, get the XML output from Facebook then parse the data using XMLDocument.

Note: you should validate the input URLs in your application.

HTML of this page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aspx-get-total-comments-total-likes-total-shares-of-facebook-url.aspx.cs" Inherits="aspx_get_total_comments_total_likes_total_shares_of_facebook_url" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td valign="top" align="left">URLs: </td>
            <td align="left"><asp:TextBox runat="server" ID="txtURLs" Width="300px"></asp:TextBox> (separated by comma)<br />
+ http://4rapiddev.com<br />
+ http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/</td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Button runat="server" ID="btnSubmit" Text="Submit" 
                    onclick="btnSubmit_Click" /></td>
        </tr>
        <tr>
            <td colspan="2"><hr /></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Request URL</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:Label runat="server" ID="lblRequestURL"></asp:Label></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Raw result from Facebook</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:HyperLink runat="server" ID="hplRawResult" Text="Click here" Target="_blank"></asp:HyperLink></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Facebook Share Statistic Information</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:Literal runat="server" ID="ltrResult"></asp:Literal></td>
        </tr>
    </table>    
    </div>
    </form>
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aspx-get-total-comments-total-likes-total-shares-of-facebook-url.aspx.cs" Inherits="aspx_get_total_comments_total_likes_total_shares_of_facebook_url" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td valign="top" align="left">URLs: </td> <td align="left"><asp:TextBox runat="server" ID="txtURLs" Width="300px"></asp:TextBox> (separated by comma)<br /> + http://4rapiddev.com<br /> + http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/</td> </tr> <tr> <td></td> <td><asp:Button runat="server" ID="btnSubmit" Text="Submit" onclick="btnSubmit_Click" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td colspan="2"><strong>Request URL</strong></td> </tr> <tr> <td colspan="2"><asp:Label runat="server" ID="lblRequestURL"></asp:Label></td> </tr> <tr> <td colspan="2"><strong>Raw result from Facebook</strong></td> </tr> <tr> <td colspan="2"><asp:HyperLink runat="server" ID="hplRawResult" Text="Click here" Target="_blank"></asp:HyperLink></td> </tr> <tr> <td colspan="2"><strong>Facebook Share Statistic Information</strong></td> </tr> <tr> <td colspan="2"><asp:Literal runat="server" ID="ltrResult"></asp:Literal></td> </tr> </table> </div> </form> </body> </html>

Feb 21, 2012Hoan Huynh

Source Code Demo page

Get Facebook Total Comments, Total Likes, Total Shares Of A Given URLPHP Get Facebook URL Total Comments, Total Likes, Total Shares
You Might Also Like:
  • PHP Get Facebook URL Total Comments, Total Likes, Total Shares
  • Get Facebook Total Comments, Total Likes, Total Shares Of A Given URL
  • PHP Get Likes Number Of Facebook Page
  • Count Total Rows For All Tables In MS SQL Server
  • Get WordPress Most Popular Posts By Total Comments
  • C# Parse Item Value And Name In XML String
  • PHP Check If User Like Facebook Page
  • C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)
  • Load And Save Facebook Profile Picture Of User
  • Create Simple Math (Mathematical) Captcha With ASP.NET C#
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

Link9 years ago CSharpFacebook, HttpWebRequest, HttpWebResponse, links.getStats, restserver, StreamReader, UrlEncode, XmlDocument, XmlElement1,095
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,196 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,069 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,846 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,323 views
C# Read Json From URL And Parse/Deserialize Json
9,815 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development