Cyber Security & Dot Net Security

Friday, December 24, 2010

C# Parse Meta Tags

You may come across an instance in your C# and ASP.NET programming where you need to 
download an external webpage and parse the meta tags... specifically, the "Title," 
"Meta Description," and "Meta Keywords."

The method below will show you how to:

    * download an external webpage
    * parse the meta title
    * parse the meta description
    * parse the meta keywords

The parsing is done using regular expressions.

NOTE: This may not be the best way of doing this, but it is a solution that you can use.

view sourceprint


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
 
namespace Tim.Examples.Classes
{
    public class WebMetaData
    {
        public string metaTitle;
        public string metaDescription;
        public string metaKeywords;
 
        public bool GetMetaTags(string url)
        {
            try{
                //get the HTML of the given page and put into a string
                string html = AcquireHTML(url);
 
                if (GetMeta(html))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                // do something with the error
                return false;
            }
        }
 
        private string AcquireHTML(string address)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            StreamReader reader;
            StringBuilder sbSource;
 
            try
            {
                // Create and initialize the web request  
                request = System.Net.WebRequest.Create(address) as HttpWebRequest;
                request.UserAgent = "your-search-bot";
                request.KeepAlive = false;
                request.Timeout = 10 * 1000;
 
                // Get response  
                response = request.GetResponse() as HttpWebResponse;
 
                if (request.HaveResponse == true && response != null)
                {
                    // Get the response stream  
                    reader = new StreamReader(response.GetResponseStream());
 
                    // Read it into a StringBuilder  
                    sbSource = new StringBuilder(reader.ReadToEnd());
 
                    response.Close();
 
                    // Console application output  
                    return sbSource.ToString();
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                response.Close();
                return "";
            }
        }
 
        private bool GetMeta(string strIn)
        {
            try
            {
                // --- Parse the title
Match TitleMatch = Regex.Match(strIn, "<title>([^<]*)</title>, 
RegexOptions.IgnoreCase | RegexOptions.Multiline);
                metaTitle = TitleMatch.Groups[1].Value;
 
                // --- Parse the meta keywords
 Match KeywordMatch = Regex.Match(strIn, "<meta name=\"keywords\" 
content=\"([^<]*)\">", 
RegexOptions.IgnoreCase | RegexOptions.Multiline);
                metaKeywords = KeywordMatch.Groups[1].Value;
 
                // --- Parse the meta description
 Match DescriptionMatch = Regex.Match(strIn, "<meta name=\"description\"
 content=\"([^<]*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                metaDescription = DescriptionMatch.Groups[1].Value;
 
                return true;
            }
            catch (Exception ex)
            {
                // do something with the error
                return false;
            }
        }
 
    }
}

Creating an AJAX Enabled WebParts in ASP.NET 2.0


1. Open Visual Studio 2005 and Create new Website
2. Select ASP.NET AJAX-Enabled Web Site then press OK (See Figure A)
Figure A
3. As what you have noticed, the ScriptManager Control is automatically added to your page. ScriptManager handles the Ajax functionality in the ASP.NET page.
4. Drag Updatepanel control below the ScriptManager (See Figure B)
Figure B.
5. From the Visual Studio Toolbox ,drag a WebPartManger inside the Updatepanel control
6. Drag two WebPartZone below the WebPartManager (See Figure C)
Figure C.
7. Drag and placed a TextBox control in the WebpartZone
8. Compile the Appication and Run the Website
9. Drag a webpart in different zones and observe what happens

As what you have noticed, you can drag and drop a webpart only once and if you would try to drag it again it will just highlight the header of the webpart that you are trying to drag. Basically by default, webparts drag and drop functionality does not supported in UpdatPanel control but they (ASP.NET team) provided a workaround on how to move webparts within UpdatPanel control.

Note: This workaround provided will ONLY work for IE browser.You need to use Visual studio 2008 / VWD 2008 with latest version of the Microsoft ASPNET Futures (AJAX Control Toolkit 3.5) in order for you to make the drag and drop feature work for all major browsers. See this link below: http://geekswithblogs.net/dotNETvinz/archive/2008/09/12/ajax-enabled-webparts-and-firefox-drag-and-drop.aspx
The Problem
ASP.NET Web Parts Drag and Drop feature and Drop down verbs menu does not operate correctly inside of a Microsoft AJAX 1.0 UpdatePanel.

Why it doesn't work?
The WebPartManager is responsible for registering an include and start up script. This script provides Web Parts and zones with various client side functionality including drag and drop and
drop down verb menus.

When a control is placed inside of an Update Panel, the script is rendered and ran on the first render, but not on subsequent renders. Due to this, the client side functionality fails.

The Workaround
The solution is simple. Inherit the WebPartManager, override the RenderClientScript Method and render the client scripts using the System.Web.UI.ScriptManager instead of the System.Web.UI.ClientScriptManager.

The System.Web.UI.ScriptManager informs Ajax of the registered client scripts and ensures that they are rendered out and executed whenever an UpdatePanel is refreshed. To achieve this, follow the steps below:

1. Right click on the Project and Add a Reference (See Figure D)
Figure D.
2. Click the Add Reference. You should be able to see the add reference window.
3. Click on the Browse Tab (See Figure E)
 Figure E.
4. Browse the Sample.Web.UI.WebParts.dll file. I have attached a zipped file together with this article for the dll.
5. Add the following Tag Mapping to the specified section of your web.config

 <configuration>
<system.web>
    <pages>
          <tagMapping>
               <add tagType="System.Web.UI.WebControls.WebParts.WebPartManager" 
mappedTagType="Sample.Web.UI.WebParts.WebPartManager, Sample.Web.UI.WebParts"/>
               </tagMapping>
    </pages>
</system.web>
</configuration>


6. Compile the Application and Run again.
7. The result can be seen in the screen shot below:



As what you have notice now, you can drag and drop webparts in different WebPartZones without refreshing the page.

Dynamically Adding TextBox Control to ASP Table in ASP.NET

 
This article shows on how to generate Table with TextBoxes dynamically based from the number of Columns and Rows entered from the TextBox control and print the values of the dynamically added TextBox on the page. See the screen shot below:

To start, let’s declare the following global variables below:
private int numOfRows = 0;

private int numOfColumns = 0;
Here’s the code block for the Generating the Tables with TextBoxes.

private void GenerateTable(int colsCount, int rowsCount)
{

        //Creat the Table and Add it to the Page
        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);

        // Now iterate through the table and add your controls
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                TextBox tb = new TextBox();

                // Set a unique ID for each TextBox added
                tb.ID = "TextBoxRow_" + i + "Col_" + j;
     
                // Add the control to the TableCell

                cell.Controls.Add(tb);

                // Add the TableCell to the TableRow

                row.Cells.Add(cell);

            }

            // Add the TableRow to the Table

            table.Rows.Add(row);
        }
As you can see from above code, we just simply create a blank table and add it to the form and then fill the table with columns and rows of TextBoxes based from the values of rowsCount and colsCount.



Now let’s call the method above on Page_Load event to recreate the Table when it post backs.
protected void Page_Load(object sender, EventArgs e)
{
         // the two paramters are declared globally

         GenerateTable(numOfColumns, numOfRows);
}
Now let’s call the method above on Button Click event (Generate Table) and pass the necessary parameters needed, see below code block:
protected void Button1_Click(object sender, EventArgs e)
{

        //Check if the inputs are numbers

        if (int.TryParse(TextBox1.Text.Trim(), out numOfColumns) && int.TryParse(TextBox2.Text.Trim(), out numOfRows))
        {

            //Generate the Table based from the inputs
            GenerateTable(numOfColumns, numOfRows);

            //Store the current Rows and Columns In ViewState as a reference value when it post backs

            ViewState["cols"] = numOfColumns;
            ViewState["rows"] = numOfRows;
        }
        else //Check if the data entered from the TextBox are numeric
        {
            Response.Write("Values are not numeric!");
        }
}
As you have noticed we used ViewState to store the current number of rows and columns that was entered to track down the current rows and columns on postbacks.
Now, since we are done on creating our Table with TextBoxes dynamically then let’s grab the values entered from the dynamic TextBox using Request.Form method. Here’s the code block below.
protected void Button2_Click(object sender, EventArgs e)
{

    //Check if ViewState values are null

    if (ViewState["cols"] != null && ViewState["rows"] != null)
    {

        //Find the Table in the page
        Table table = (Table)Page.FindControl("Table1");

        if (table != null)
        {

            //Re create the Table with the current rows and columns

            GenerateTable(int.Parse(ViewState["cols"].ToString()), int.Parse(ViewState["rows"].ToString()));

            // Now we can loop through the rows and columns of the Table and get the values from the TextBoxes

            for (int i = 0; i < int.Parse(ViewState["rows"].ToString()) ; i++)
            {
                for (int j = 0; j < int.Parse(ViewState["cols"].ToString()); j++)
                {

                    //Print the values entered
                    if (Request.Form["TextBoxRow_" + i + "Col_" + j] != string.Empty)
                    {
                        Response.Write(Request.Form["TextBoxRow_" + i + "Col_" + j] + "<BR/>");
                    }
                }
            }
        }
    }
}
As you noticed, we used Request.Form to get the the values from the Dynamic TextBox. We cannot use FindControl since the Control is cleared and recreated on every postbacks thus we cannot reference the controls ID.