How To Call A URL In ASP.NET

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. 
  1. using System.Net;  
  2. using System.IO;  
Step 2. Copy and paste given function in your .cs file. 
  1. public void callurl(string url)  
  2. {  
  3. WebRequest request = HttpWebRequest.Create(url);  
  4. WebResponse response = request.GetResponse();  
  5. StreamReader reader = new StreamReader(response.GetResponseStream());  
  6. string urlText = reader.ReadToEnd(); // it takes the response from your url. now you can use as your need  
  7. Response.Write(urlText.ToString());  
  8. }  
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. 
  1. callurl("http://google.com");