Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Get Site Title and Meta Tag Information through URL

Article:
Viewed:  1152 
Posted On:  03/02/2015 01:00:50 
How we can know about a site through it's URL only in asp.net c# 

In this article I am going to explain how we can get title and meta tag information of a web site through its URL.

For this I need to add HtmlAgilityPack reference.

So right click on your application solution explorer and select Manage NuGet Packages:

1.png

Image 1.

You can also download this dll from below link:

2.png

Image 2.

Now my aspx is:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="URLInformation.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get Website Information through URL</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="border: solid 15px blue; padding: 7px; width: 100%;">
                <tr>
                    <td style="background-color: green; color: white; text-align: center;" colspan="2">
                        <h3>Get Title and Description of a Web Site</h3>
                    </td>
                </tr>
                <tr style="background-color: skyblue;">
                    <td style="vertical-align: top; padding: 10px;">Type URL:
                    </td>
                    <td style="padding: 10px;">
                        <asp:TextBox ID="txtURL" runat="server" AutoPostBack="True" Width="450px"></asp:TextBox>
                        <br />
                        <br />
                        <asp:Button ID="btnGetInfo" runat="server" Text="Get Information" OnClick="btnGetInfo_Click" />
                    </td>
                </tr>
                <tr style="background-color: orange;">
                    <td style="padding: 10px;">Title:
                    </td>
                    <td style="padding: 10px;">
                        <asp:TextBox ID="txtTiltle" runat="server" Width="450px"></asp:TextBox>
                    </td>
                </tr>
                <tr style="background-color: orange;">
                    <td style="padding: 10px;">Description:
                    </td>
                    <td style="padding: 10px;">
                        <asp:TextBox ID="txtMetaTagDesc" runat="server" Width="450px" Height="70px" TextMode="MultiLine"></asp:TextBox>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


My aspx.cs code is:

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.Text.RegularExpressions;
using HtmlAgilityPack;
 
namespace URLInformation
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void btnGetInfo_Click(object sender, EventArgs e)
        {
            String _url = txtURL.Text;
            WebClient x = new WebClient();
            string source = x.DownloadString(_url);
            txtTiltle.Text = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
 
            GetURLMetaTag(_url);
        }
 
        private void GetURLMetaTag(string url)
        {
            var webGet = new HtmlWeb();
            var document = webGet.Load(url);
 
            var metaTags = document.DocumentNode.SelectNodes("//meta");
 
            if (metaTags != null)
            {                
                 foreach (var tag in metaTags)
                {
                    if (tag.Attributes["name"] != null && tag.Attributes["content"] != null && tag.Attributes["name"].Value == "description")
                    {
                        txtMetaTagDesc.Text = tag.Attributes["content"].Value;
                    }
                }
            }
            else
            {
                txtMetaTagDesc.Text = "";
            }
        }
    }

}

Now run the application:


3.png

Image 3.

4.png

Image 4.

5.png

Image 5.

  Comment:
 
By dotnet  On  06/04/2016 01:35:37

What is NuGet Package Manager
http://allittechnologies.blogspot.in/2016/03/what-is-aspnet-nuget-package-manager.html
Convert Post Title Into UrL
http://allittechnologies.blogspot.in/2015/06/how-to-convert-post-title-into-url-by-using-asp-net-csharp-programming.html
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us