New Fresh deep-dives on .NET, Azure & AI now publish on blog.msdevbuild.com Read the latest →
Web

How To Read Web Page HTML Code With ASP.NET?

By   Monday, January 31, 2011 comments
reads
React:
If data are not available in some kind of XML format (like RSS or web services), sometime you need to deal with HTML output of web page. HTML is usually much harder to analyze than XML because you need to write your own parser which is different for every web site. But, the first step, reading of HTML output of web page is pretty simple. To get HTML of web page you need only few lines of code.

To start, place two TextBox controls named txtURL and txtPageHTML, and one button control on web form, like in image bellow:
Image 1: Web form for getting page HTML at design time
Now, on button's click event function, place this code:

[ C# ]
// We need these namespaces

using System;
using System.Text;
using System.Net;

public partial class DefaultCS : System.Web.UI.Page
{
protected void btnGetHTML_Click(object sender, EventArgs e)
{
// We'll use WebClient class for reading HTML of web page
WebClient MyWebClient = new WebClient();
// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (txtURL.Text != "")
{
PageHTMLBytes = MyWebClient.DownloadData(txtURL.Text);
// Convert result from byte array to string
// and display it in TextBox txtPageHTML
UTF8Encoding oUTF8 = new UTF8Encoding();
txtPageHTML.Text = oUTF8.GetString(PageHTMLBytes);
}
}
}
[ VB.NET ]
' We need these namespaces

Imports System
Imports System.Text
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnGetHTML_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnGetHTML.Click

' We'll use WebClient class for reading HTML of web page

Dim MyWebClient As WebClient = New WebClient()

' Read web page HTML to byte array

Dim PageHTMLBytes() As Byte
If txtURL.Text <> "" Then
PageHTMLBytes = MyWebClient.DownloadData(txtURL.Text)

' Convert result from byte array to string

' and display it in TextBox txtPageHTML

Dim oUTF8 As UTF8Encoding = New UTF8Encoding()
txtPageHTML.Text = oUTF8.GetString(PageHTMLBytes)
End If
End Sub
End Class

Now you can start sample project, type some valid URL in first TextBox control and click to "btnGetHTML" button. Code listed above will return HTML code of requested URL and display it in second text box, like in image bellow:


Image 2: HTML Code is read and shown in text box

As you see, loading of HTML code of web page is relatively easy. Analyzing of this data is much harder and depends of page structure.
SJ
Suthahar Jegatheesan

Senior Solutions Architect · Microsoft Azure, AI & Mobile. Author of the Xamarin Q&A Book.

More in this series

1 comment:

Practical Learn .NET, Azure & AI

sponsored

What Colleagues Say

“Suthahar is one of the most thorough and knowledgeable cloud architects I’ve worked with. His depth on Azure and ability to make complex mobile solutions work cross-platform is remarkable. He always delivers beyond expectations.”

AT Arun T. Senior Engineering Manager · Direct Manager

“I’ve followed Suthahar’s MSDEVBUILD blog for years — practical, deep, always current. Working alongside him confirmed what I suspected: he’s as sharp in person as on paper, and a genuinely generous mentor and architect.”

RK Rajesh K. Lead Developer · Mindtree

Certifications & Recognition

AZ-300 · Azure Architect Technologies AZ-301 · Azure Architect Design AZ-104 · Azure Administrator Associate AI-100 · Designing Azure AI Solutions DP-900 · Azure Data Fundamentals MCPD · Windows Phone Certified SAFe® 4 Scrum Master

Technical Expertise

☁ Cloud Platforms Microsoft Azure — Expert · 18 yrs App Services · AKS · Functions · Cosmos DB · APIM · Entra ID AWS & Google Cloud — Advanced
πŸ€– AI & Automation Azure OpenAI / GenAI — Advanced · 5 yrs RAG Pipelines · Bot Framework · Cognitive Services · Vision & Speech AI · ML.NET
πŸ“± Mobile Architecture .NET MAUI & Xamarin — Expert · 12 yrs Flutter · Native iOS/Android · Offline-First · Secure Storage & Auth
πŸ”’ Identity & Security Microsoft Entra ID — Expert OAuth 2.0 · OIDC · Zero Trust · Conditional Access · RBAC · Key Vault

Speaking & Community

Microsoft User Group Malaysia Recurring speaker on Azure architecture, AI integration and .NET mobile development across Malaysia and APAC.
Global Azure Bootcamp — Chennai Presented “AI into Xamarin Mobile Apps” at GAB Chennai, part of the worldwide Global Azure community event.
AI & Azure OpenAI Community Events Workshops and live demos on RAG pipelines, AI agents and intelligent workflow automation since 2023.
Mobile & Cloud Workshops — Coimbatore Multi-day deep-dive sessions at GRG Computer Technology, KG Microsoft Innovation Center and RVS College of Arts and Science.
500+Blog Articles
50+Conference Talks
10K+Monthly Readers
30Public Repositories
3Continents Delivered
MSDEVBUILD Assistant Ask about Suthahar, Azure, AI or mentorship
Hi! I’m a simple FAQ bot for MSDEVBUILD. Tap a question below or type your own.