Get Title and Meta Description of Live URL

Introduction
 
This article explains how to get "title and meta description" of a URL using ASP.Net. To do that just use the following procedure.
 
Step 1
 
Download "Html Agility Pack" from http://html-agility-pack.net/?z=codeplex
 
I will explain how to use that downloaded file later in this article.
 
Step 2
 
Make the design to get the "title and meta description" of a URL.
 
Code for the design
 
Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>    
  2.      
  3. <!DOCTYPE html>    
  4.      
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head id="Head1" runat="server">    
  7.     <title>Get Title and Descripition of Live URl</title>    
  8.     <style type="text/css">    
  9.         .auto-style1 {    
  10.             width: 100%;    
  11.         }    
  12.         .auto-style2 {    
  13.             height: 15px;    
  14.         }    
  15.         .auto-style3 {    
  16.             height: 15px;    
  17.             width: 172px;    
  18.         }    
  19.         .auto-style4 {    
  20.             width: 172px;    
  21.         }    
  22.     </style>    
  23. </head>    
  24. <body>    
  25.     <form id="form1" runat="server">    
  26.     <div>    
  27.     <h2>Get Title and Descripition of Live URl</h2>    
  28.          
  29.     </div>    
  30.        <table style="border:groove #00FF99">    
  31.             <tr>    
  32.                 <td class="auto-style3"><h2 style="width: 167px; height: 20px">Enter Url</h2></td>    
  33.                 <td class="auto-style2">    
  34.                     <asp:TextBox ID="TextBox1" runat="server" Width="530px" style="margin-left: 0px" Height="28px"></asp:TextBox>    
  35.                 </td>    
  36.             </tr>    
  37.             <tr>    
  38.                 <td class="auto-style4"><h2>Title</h2></td>    
  39.                 <td>    
  40.                     <asp:TextBox ID="TextBox2" runat="server" Width="528px" Height="29px" Font-Bold="True" Font-Names="Times New Roman" Font-Size="Medium"></asp:TextBox>    
  41.                 </td>    
  42.             </tr>    
  43.             <tr>    
  44.                 <td class="auto-style4"><h2>Descripition</h2></td>    
  45.                 <td>    
  46.                     <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Width="531px" Height="90px" Font-Bold="True" Font-Names="Times New Roman" Font-Size="Medium"></asp:TextBox>    
  47.                 </td>    
  48.             </tr>    
  49.             <tr>    
  50.                 <td colspan="2">    
  51.                   <br />    
  52.  <asp:Button ID="Button1" runat="server"  Text="Get" BackColor="#CC6600" ForeColor="#FFFF99" Height="33px" Width="85px" OnClick="Button1_Click"  />    
  53.                 </td>    
  54.             </tr>    
  55.         </table>    
  56.     </form>    
  57. </body>    
  58. </html>  
For the code above, the output looks like:
 
 
 
Step 3
 
Add the reference of the preceding download "HtmlAgilitypack".
 
Now let's move to the "Default.aspx.cs" code.
 
In this you require the following namespaces:
  1. using System;        
  2. using System.Net;        
  3. using System.Text.RegularExpressions;        
  4. using HtmlAgilityPack;  
The last namespace is available to you, when you add the reference of the above download "HtmlAgilitypack". Now let's concern ourselves with the C# code
 
The code given below enables you to retrieve the title and meta description of a live "URL" in ASP.Net.
  1. using System;  
  2. using System.Net;  
  3. using System.Text.RegularExpressions;    // Provides access to regular expression related api.  
  4. using HtmlAgilityPack;  
  5.   
  6. public partial class _Default : System.Web.UI.Page  
  7. {  
  8.     protected void Page_Load(object sender, EventArgs e)  
  9.     {  
  10.     }  
  11.     protected void Button1_Click(object sender, EventArgs e)  
  12.     {  
  13.         String url = TextBox1.Text;  
  14.         WebClient x = new WebClient();  
  15.         string sourcedata = x.DownloadString(url);  
  16.         TextBox2.Text = Regex.Match(sourcedata, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;  
  17.         GetMetaTagValue(url);  
  18.     }  
  19.   
  20.     private void GetMetaTagValue(string url)  
  21.     {  
  22.         var getHtmlDoc = new HtmlWeb();  
  23.         var document = getHtmlDoc.Load(url);  
  24.         var metaTags = document.DocumentNode.SelectNodes("//meta");  
  25.         if (metaTags != null)  
  26.         {  
  27.             foreach (var sitetag in metaTags)  
  28.             {  
  29.                 if (sitetag.Attributes["name"] != null && sitetag.Attributes["content"] != null && sitetag.Attributes["name"].Value == "description")  
  30.   
  31.                 {  
  32.   
  33.                     TextBox3.Text = sitetag.Attributes["content"].Value;  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38. }  
When you write the live "URL" in the "Enter URL" corresponding TextBox then click on the button the output looks like:
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all