Net-informations.com
SiteMap  | About    

How to read URL Content from webserver

The .NET framework provides two namespaces, System.Net and System.Net.Sockets for network programming. The System.Net classes use to communicate with other applications by using the HTTP, TCP, UDP and Sockets.

When we want to read the content of an HTML page from a remote webserver in C# we are using WebRequest and WebResponse Classes. WebResponse return a StreamReader and we can get the content from StreamReader . The following C# program shows how to read the content of an HTML page using WebRequest and WebResponse Classes.




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());
            }
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.