Fri, 17 May 2024


How to read a content of a web page using C# and store into a string variable?

By: Peter, NetArt Media
Sun, 7 February 2021

// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create("http://zamov.online.fr");

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();

// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode );

string strResponse=readStream.ReadToEnd();

Console.WriteLine(strResponse);

readStream.Close();

// Release the resources of response object.
myWebResponse.Close();


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox