Reading Web Pages : Web Client « Network « C# / C Sharp

Reading Web Pages

A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3

// 32 - .NET Frameworks Overview\Reading Web Pages
// copyright 2000 Eric Gunnerson
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

class QuoteFetch
    public QuoteFetch(string symbol)
        this.symbol = symbol;
    public string Last
            string url = "";
            url += symbol;
    string ReadUrl(string url)
        Uri uri = new Uri(url);
        //Create the request object
        WebRequest req = WebRequest.Create(uri);
        WebResponse resp = req.GetResponse();
        Stream stream = resp.GetResponseStream();
        StreamReader sr = new StreamReader(stream);
        string s = sr.ReadToEnd();
    void ExtractQuote(string s)
        // Line like: "Last</TD><TD ALIGN=RIGHT NOWRAP><B>&nbsp;78 3/16"
        Regex lastmatch = new Regex(@"Last\D+(?<last>.+)<\/B>");
        last = lastmatch.Match(s).Groups[1].ToString();
    string    symbol;
    string    last;

public class ReadingWebPages
    public static void Main(string[] args)
        if (args.Length != 1)
        Console.WriteLine("Quote <symbol>");
            // GlobalProxySelection.Select = new DefaultControlObject("proxy", 80);
            QuoteFetch q = new QuoteFetch(args[0]);
            Console.WriteLine("{0} = {1}", args[0], q.Last);

Related examples in the same category

1.Basic WebClient
2.Save web page from HttpWebResponse
3.Displays the resource specified
4.My Web Client
5.Handle network exceptionsHandle network exceptions
6.Use WebClient to download information into a fileUse WebClient to download information into a file
7.Use LastModifiedUse LastModified
8.Examine the headersExamine the headers
9.Access the InternetAccess the Internet
10.NetworkCredential Cache Test
11.NetworkCredential test
12.Download Data Test
13.Download File Test
14.Web GetWeb Get
15.Web Client Upload Values Test
16.Web Client Upload Data Test 2
17.Web Client Response Headers Test
18.Web Client Open Write Test
19.Web Client Open Read Test
20.Gets or sets the network credentials that are sent to the host and used to authenticate the request.
21.Download String
22.Web Downloader
23.Fetches a web page
24.Http Get
25.Returns a site relative HTTP path from a partial path starting out with a ~.
26.Get Web Text
27.HTTP error code
28.Stores/save an image from the web to disk.