How to read URL Content from webserver

The .NET framework offers two namespaces, namely System.Net and System.Net.Sockets, specifically designed for network programming. These namespaces provide classes and functionalities to facilitate communication between applications using various Internet protocols such as HTTP, TCP, UDP, and Sockets.

WebResponse class

When it comes to reading the content of an HTML page from a remote web server in C#, the WebRequest and WebResponse classes come into play. The WebRequest class allows you to create a request to a specific URL, while the WebResponse class handles the response received from the web server.

Upon receiving the response from the web server, the WebResponse class provides a StreamReader object, which allows you to read the content of the response. Using the StreamReader, you can easily retrieve and process the HTML content of the web page.

The following C# program shows how to read the content of an HTML page using WebRequest and WebResponse Classes.

Full Source C#
using System; using System.Windows.Forms; using System.Net; using System.IO; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { StreamReader inStream ; WebRequest webRequest ; WebResponse webresponse ; webRequest = WebRequest.Create(textBox1.Text); webresponse = webRequest.GetResponse(); inStream = new StreamReader(webresponse.GetResponseStream()); textBox2.Text = inStream.ReadToEnd(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }


Using the WebRequest, WebResponse, and StreamReader classes, you can establish a connection to a remote web server, send a request, receive the response, and extract the desired content from the response stream. This capability is crucial for various web-related tasks, such as web scraping, data extraction, or simply retrieving the content of web pages programmatically.