Friday, August 30, 2013

How to Get Title, Description, Keywords Meta Tags and Image from URL in Asp.net

Introduction: In this article I will explain how we can Get Title, Description, Keywords Meta Tags and Image from URL in Asp.net.

Description:
Take a new project and put the DLL in Bin folder of Project. Bulid the project. After that add a new webform to project and design as mention below:

<table><tr><td style="width:200px;">Website Url:</td><td> <asp:TextBox ID="txtwebsiteurl" runat="server" Width="831px"></asp:TextBox></td>
</tr>
<tr><td>&nbsp;</td><td><asp:Button ID="btnGet" runat="server" Font-Bold="True" Font-Italic="True"
            onclick="btnGet_Click" Text="Fetch Details" /></td></tr>
            <tr><td>
            Title :</td><td> <asp:TextBox ID="txttitle" runat="server" Width="831px"></asp:TextBox></td></tr>
            <tr><td> Description:</td><td><asp:TextBox ID="txtdescription" runat="server" Height="118px" TextMode="MultiLine"
            Width="831px"></asp:TextBox></td></tr>
             <tr><td> Keywords:</td><td><asp:TextBox ID="txtkeywords" runat="server" Width="831px" TextMode="MultiLine"></asp:TextBox></td></tr>
            <tr><td> Image :</td><td> <asp:Image ID="Image1" runat="server" Height="250px" Width="831px" />           
            </td></tr>
            </table>

On button click (.aspx.cs) write the below given code:

using System.Net;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.IO;

protected void btnGet_Click(object sender, EventArgs e)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(txtwebsiteurl.Text));
            request.Method = WebRequestMethods.Http.Get;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            String ResponseString = reader.ReadToEnd();
            response.Close();
            HtmlDocument DOC = new HtmlDocument();
            DOC.LoadHtml(ResponseString);
            String title = (from x in DOC.DocumentNode.Descendants()
                            where x.Name.ToLower() == "title"
                            select x.InnerText).FirstOrDefault();
            String desc = (from x in DOC.DocumentNode.Descendants()
                           where x.Name.ToLower() == "meta"
                           && x.Attributes["name"] != null
                           && x.Attributes["name"].Value.ToLower() == "description"
                           select x.Attributes["content"].Value).FirstOrDefault();
            String keywords = (from x in DOC.DocumentNode.Descendants()
                               where x.Name.ToLower() == "meta"
                               && x.Attributes["name"] != null
                               && x.Attributes["name"].Value.ToLower() == "keywords"
                               select x.Attributes["content"].Value).FirstOrDefault();
            List<String> imgs = (from x in DOC.DocumentNode.Descendants()
                                 where x.Name.ToLower() == "img"
                                 select x.Attributes["src"].Value).ToList<String>();

            txttitle.Text = title;
            txtdescription.Text = desc;
            txtkeywords.Text = keywords;
            Image1.ImageUrl = imgs[0];
        }
        catch (Exception ex)
        {
        }

    }
now run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment