We can use WebRequest and WebResponse classes in ASP.NET and C# to call a URL and read its content.
Step 1. Import the following namespaces in your application.
- using System.Net;
- using System.IO;
Step 2. Copy and paste given function in your .cs file.
- public void callurl(string url)
- {
- WebRequest request = HttpWebRequest.Create(url);
- WebResponse response = request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string urlText = reader.ReadToEnd();
- Response.Write(urlText.ToString());
- }
In the above code, a WebRequest object is created by passing a URL. The GetResponse returns a WebResponse object and that is the stream with the content of a web page. After that, the content is read in a string.
Step 3. Call this function on a button click or anywhere you need it.
- callurl("http://google.com");