Monday 18 February 2013

Check whether a Particular Word is Available in a URL or Not


Check whether a Particular Word is Available in a URL or Not


You can Use the following Code to check whether a Particular Word is available in a Particular URL or not...


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckViewSource.aspx.cs" ValidateRequest="false" Inherits="CheckViewSource" %>

<!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>Check ViewSource</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Enter URL:"></asp:Label>
        <asp:TextBox ID="txtURL" runat="server" Height="20px" Width="300px"></asp:TextBox><br />
        <br />
        <asp:Button ID="btnSource" runat="server" OnClick="btnSource_Click" Text="Check Source" /><br />
        <br />
        <asp:TextBox ID="txtSource" runat="server" Height="300px" TextMode="MultiLine" Width="600px"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Find this Word in the Above Source:"></asp:Label>
        <asp:TextBox ID="txtCheck" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Text="Check Word" /><br />
        <br />
        <asp:Label ID="lblDisplay" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label><br />
        <br />
    
    </div>
    </form>
</body>
</html>

and in the Code-Behind:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net; 
using System.IO;

public partial class CheckViewSource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSource_Click(object sender, EventArgs e)
    {          
            string URL=txtURL.Text;
            // Create a request for the URL.
            WebRequest request = WebRequest.Create(URL);
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream ds= response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader rd= new StreamReader (ds);
            // Read the content. 
            string responser = rd.ReadToEnd ();
            txtSource.Text=responser;
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        string URLSource=Server.HtmlEncode(txtSource.Text);
        string Check=txtCheck.Text;
        bool val=URLSource.Contains(Check);
        if(val==true)
        {
           lblDisplay.Text="The Word: **"+txtCheck.Text+"** is present in the above Source";
        }
        else
        {
           lblDisplay.Text="The Word: **"+txtCheck.Text+"** is not present in the above Source";
        }
    }
}

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top