HTTP is the primary transport mechanism for communicating with resources over the World-Wide-Web. A developer will often want to obtain web pages for different reasons to include: search engine page caching, obtaining info on a particular page, or even implementing browser-like capabilities. To help with this task, the .NET Framework includes classes that make this easy.
Getting an HTTP Page
The HTTP classes in the .NET framework are HTTPWebRequest and HTTPWebResponse. The steps involved require specifying a web page to get with a HTTPWebRequest object, performing the actual request, and using a HTTPWebResponse object to receive the page. Thereafter, you would use stream operations to extract page information. Listing 1 demonstrates how this process works.
WebFetch.cs
Code: Select all
using System;
using System.IO;
using System.Net;
using System.Text;
/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
static void Main(string[] args)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("https://robot.lk/call.php?para1=1234");
// execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
Console.WriteLine(sb.ToString());
}
}
The first step in getting a web page is to instantiate a HttpWebRequest object. This occurs when invoking the static Create() method of the WebRequest class. The parameter to the Create() method is a string representing the URL of the web page you want. A similar overload of the Create() method accepts a single Uri type instance. The Create() method returns a WebRequest type, so we need to cast it to an HttpWebRequest type before assigning it to the request variable. Here's the line creating the request object:
Code: Select all
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("https://robot.lk/call.php?para1=1234");
Code: Select all
// execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Code: Select all
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
Code: Select all
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
Code: Select all
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
Summary
The HttpWebRequest and HttpWebResponse classes from the .NET Base Class Library make it easy to request web pages over the internet. The Httprequest object identifies the Web page to get and contains a GetResponse() method for obtaining a HttpWebResponse object. With a HttpWebResponse object, we retrieve a stream to read bytes from. Iterating until all the bytes of a Web page are read, translating bytes to strings, and holding the string, makes it possible to obtain the entire Web page.