Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Get WebPage SourceCode

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Apr 2011CPOL1 min read 10.2K   2   1
Here Is A different way to get the source . This method will explain a small amount of what we can do with the .Net language, it will show you a simple way to use an interface. <Please Note: If You are familiar with how to do so please do not downvote the tip/trick as there are...
Here Is A different way to get the source <Long Version>. This method will explain a small amount of what we can do with the .Net language, it will show you a simple way to use an interface. <Please Note: If You are familiar with how to do so please do not downvote the tip/trick as there are people who don't, but I do encourage anyone to build onto or post a better method of doing such> First we have our WebData Class which will return the source of the page, inside this class is where our interface and SourceCode class which implements our Source interface. Here is what the class looks like

C#
internal abstract class WebData
        {
            internal static Source HtmlSource { get; set; }
            private static String webSource(String url)
            {
                return Encoding.ASCII.GetString(new WebClient().DownloadData(url));
            }
            internal interface Source
            {
                String Get(String url);
            }
            internal class SourceCode : Source
            {
                public String Get(String url)
                {
                    try
                    {
                        if (HtmlSource == null)
                            return String.Empty;
                        else
                            return webSource(url);
                    }
                    catch { return String.Empty; }
                }
            }
        }


Now outside of you code block to call this class just add this line of code.

C#
private WebData.Source source = new WebData.SourceCode();


I have my WebBrowser which is webBrowser1 set within a tabpage in a tabControl. this TabPage is the only tabPage in the TabControl and I have a a custom TabPage Control. the code for this custom TabPage Looks like this

C#
internal class SourcePage : TabPage
        {
            private WebBrowser sourceBrowser = new WebBrowser();
            internal SourcePage(string source)
            {
                this.Controls.Add(sourceBrowser);
                sourceBrowser.Dock = DockStyle.Fill;
                sourceBrowser.DocumentText = source;                
                sourceBrowser.IsWebBrowserContextMenuEnabled = false;
                sourceBrowser.WebBrowserShortcutsEnabled = false;
            }
            //This method is for Demonstration purposes, it will remove the tab page from the TabControl automatically
            // when leaving this tabpage. Ex when you go back to Tapbage holding the default webpage
            protected override void OnLeave(EventArgs e)
            {
                base.OnLeave(e);
                base.Dispose();
            }
        }


Now To call this method you can place with a buttonClick event, menuClick and so forth. Here is how to call the new tabpage with the source to display in the the tabcontrol.

C#
private void getSourceMenuItem_Click(object sender, EventArgs e)
        {
            WebData.HtmlSource = source;
            string SourceData = WebUtility.HtmlEncode(source.Get(webBrowser1.Url.ToString()));           
            tabControl1.TabPages.Add(new SourcePage(SourceData));
        }


Here is a complete copy and paste to check how it works below

C#
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private MenuStrip menuStrip1;
        private ToolStripMenuItem getSourceMenuItem;
        private Panel panel1;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private WebBrowser webBrowser1;
        private Panel panel2;
        private Panel panel3;
        private TextBox textBox1;
        private WebData.Source source = new WebData.SourceCode();
        public Form1()
        {
            InitializeComponent();
            webBrowser1.GoHome();
        }
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.getSourceMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.panel1 = new System.Windows.Forms.Panel();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.menuStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.getSourceMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(806, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // getSourceMenuItem
            // 
            this.getSourceMenuItem.Name = "getSourceMenuItem";
            this.getSourceMenuItem.Size = new System.Drawing.Size(76, 20);
            this.getSourceMenuItem.Text = "Get Source";
            this.getSourceMenuItem.Click += new System.EventHandler(this.getSourceMenuItem_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.tabControl1);
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 24);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(806, 345);
            this.panel1.TabIndex = 1;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 35);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(806, 310);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.webBrowser1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(798, 284);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // webBrowser1
            // 
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webBrowser1.Location = new System.Drawing.Point(3, 3);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(792, 278);
            this.webBrowser1.TabIndex = 0;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.panel3);
            this.panel2.Controls.Add(this.textBox1);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(806, 35);
            this.panel2.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.panel3.Dock = System.Windows.Forms.DockStyle.Right;
            this.panel3.Location = new System.Drawing.Point(757, 0);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(49, 35);
            this.panel3.TabIndex = 1;
            // 
            // textBox1
            // 
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(0, 6);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(689, 20);
            this.textBox1.TabIndex = 0;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(806, 369);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        internal class SourcePage : TabPage
        {
            private WebBrowser sourceBrowser = new WebBrowser();
            internal SourcePage(string source)
            {
                this.Controls.Add(sourceBrowser);
                sourceBrowser.Dock = DockStyle.Fill;
                sourceBrowser.DocumentText = source;
                HtmlDocument hd = sourceBrowser.Document;
                sourceBrowser.IsWebBrowserContextMenuEnabled = false;
                sourceBrowser.WebBrowserShortcutsEnabled = false;
            }
            //This method is for Demonstration purposes, it will remove the tab page from the TabControl automatically
            // when leaving this tabpage. Ex when you go back to Tapbage holding the default webpage
            protected override void OnLeave(EventArgs e)
            {
                base.OnLeave(e);
                base.Dispose();
            }
        }
        internal abstract class WebData
        {
            internal static Source HtmlSource { get; set; }
            private static String webSource(String url)
            {
                return Encoding.ASCII.GetString(new WebClient().DownloadData(url));
            }
            internal interface Source
            {
                String Get(String url);
            }
            internal class SourceCode : Source
            {
                public String Get(String url)
                {
                    try
                    {
                        if (HtmlSource == null)
                            return String.Empty;
                        else
                            return webSource(url);
                    }
                    catch { return String.Empty; }
                }
            }
        }
        private void getSourceMenuItem_Click(object sender, EventArgs e)
        {
            WebData.HtmlSource = source;
            string SourceData = WebUtility.HtmlEncode(source.Get(webBrowser1.Url.ToString()));           
            tabControl1.TabPages.Add(new SourcePage(SourceData));
        }
        internal static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}


I hope that you enjoy the tip/trick and if you like it please vote 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralReason for my vote of 5 nice example :) thanks for sharing Pin
cSNewb22-Apr-11 5:59
cSNewb22-Apr-11 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.