You need to get the HTML returned from a web server in order to examine it for items of interest. For example, you could examine the returned HTML for links to other pages or for headlines from a news site.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetHtmlFromUrl("http://www.google.com"));
Console.Read();
}
public static string HostName2IP(string hostname)
{
// Resolve the hostname into an iphost entry using the Dns class.
IPHostEntry iphost = System.Net.Dns.GetHostEntry(hostname);
// Get all of the possible IP addresses for this hostname.
IPAddress[] addresses = iphost.AddressList;
// Make a text representation of the list.
StringBuilder addressList = new StringBuilder();
// Get each IP address.
foreach (IPAddress address in addresses)
{
// Append it to the list.
addressList.AppendFormat("IP Address: {0};", address.ToString());
}
return addressList.ToString();
}
public static string GetHtmlFromUrl(string url)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException("url", "Parameter is null or empty");
string html = "";
HttpWebRequest request = GenerateHttpWebRequest(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Get the response stream.
Stream responseStream = response.GetResponseStream();
// Use a stream reader that understands UTF8.
using (StreamReader reader =
new StreamReader(responseStream, Encoding.UTF8))
{
html = reader.ReadToEnd();
}
}
return html;
}
public static HttpWebRequest GenerateHttpWebRequest(string UriString)
{
// Get a Uri object.
Uri Uri = new Uri(UriString);
// Create the initial request.
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Uri);
// Return the request.
return httpRequest;
}
}
}