Friday, June 5, 2009

How to open other website URL content in my aspx page

Few days ago i have a requirement that client wants to display category wise sites in his home page. To do that at first what i need? I need to display an external URL or website in my aspx page. After a short search i found an example on this by devasp. For your learning i want to share the code with you.

Add the panel into your aspx page :
<asp:panel id="mainpanel" BorderStyle="Dotted" BorderColor="orange" BorderWidth="2"
style="POSITION: absolute;top:3px;left:3px" runat="server" Width="99%"></asp:panel>


protected void Page_Load(object sender, EventArgs e)
{
Uri weburi = new Uri(http://www.gmail.com/);
StringBuilder str = new StringBuilder();
string temp = "";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(weburi);
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
StreamReader webstream = new StreamReader(webresponse.GetResponseStream(), Encoding.ASCII);
while ((temp = webstream.ReadLine()) != null)
str.Append(temp +
"\r\n");
webstream.Close();
mainpanel.Controls.Add(
new System.Web.UI.LiteralControl(str.ToString()));
}
* Don't forget to add namespace: using System.Text;using System.Net;using System.IO;

Output:


Common Error:
1. Invalid URI: The format of the URI could not be determined.
Solution:
Supply full URL like http://www.gmail.com/ instead of http://www.gmail.com/

2 comments:

  1. sir i want to talk to you.. is it possible... please replay me.. to.rohan@yahoo.com

    ReplyDelete
  2. error in Uri
    weburi = new Uri(http://www.gmail.com/);

    (invalid expression terms ':') after http:

    help me please

    ReplyDelete

Write your Comment: