google

Web response to display data from another website

| Wednesday, March 20, 2013
Data from another website can be displayed in our own website using simple "WebRequest" and "WebResponse".WebRequest uses URL of the another website which can be passed hard coded or dynamically.

Add a Label control on the webpage.

 <asp:Content ID="Content2" ContentPlaceHolderID="generalContent" runat="Server">  
   <asp:Label ID="lblDisplay" runat="server">   
   </asp:Label>  
 </asp:Content>  

Add following code in Page_Load or any other event like button click etc.

 WebRequest wReq = WebRequest.Create("https://www.youtube.com/watch?v=CGu8W4cFlUw");  
       WebResponse mRes = wReq.GetResponse();  
       StreamReader sr = new StreamReader(mRes.GetResponseStream());  
       string sHTML = sr.ReadToEnd();  
       sr.Close();  
       mRes.Close();  
       if (sHTML != string.Empty && sHTML != null)  
       {  
         lblDisplay.Text = sHTML;  
       }  

Add required namespaces in the cs file

 using System;  
 using System.Collections.Generic;  
 using System.IO;  
 using System.Linq;  
 using System.Net;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  

and That's it.

Enjoy!!!

The output in the browser will be


1 comments:

Next Prev
▲Top▲