Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Network - How to retrieve a webpage using ASP.NET 2.0 and C# .NET
How to retrieve a webpage using ASP.NET 2.0 and C# .NET

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Network Tutorial

This tutorial will show you how to retrieve an external webpage using the .NET System.Net class, ASP.NET 2.0 and C#.NET

The .NET Framework offers a number of types that makes accessing resources on the network easy.

To display an external webpage we will need to use the System.IO, System.Net, and System.Text namespaces.

using System.IO
using System.Net
using System.Text

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

We'll put our code in the Page_Load() event.

When the Page_Load() event fires it instantiates a new WebRequest object with the URL that we wish to retrieve. We then execute the GetResponse() method of this new object which returns a WebResponse object. After this is complete it is simply a matter of executing the GetResponseStream() method of the WebResponse object and reading the stream that is returned.

protected void Page_Load(object sender, EventArgs e)
{

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse resp = req.GetResponse();

Stream s = resp.GetResponseStream();
StreamReader sr = new StreamReader(s,Encoding.ASCII);
string doc = sr.ReadToEnd();

lblStatus.Text = doc;

}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
<tr>
<td height="50" align="center" class="lgHeader1">How to Display Data using the DataList control ASP.NET
2.0 and C#</td>
</tr>
</table>
<br />
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> External WebPage:</td>
<td align="center" bgcolor="#FFFFFF">
&nbsp;
&nbsp;<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse resp = req.GetResponse();

Stream s = resp.GetResponseStream();
StreamReader sr = new StreamReader(s,Encoding.ASCII);
string doc = sr.ReadToEnd();

lblStatus.Text = doc;

}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks