/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 32 - .NET Frameworks OverviewReading 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
{
get
{
string url = "http://moneycentral.msn.com/scripts/webquote.dll?ipage=qd&Symbol=";
url += symbol;
ExtractQuote(ReadUrl(url));
return(last);
}
}
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();
return(s);
}
void ExtractQuote(string s)
{
// Line like: "Last</TD><TD ALIGN=RIGHT NOWRAP><B>&nbsp;78 3/16"
Regex lastmatch = new Regex(@"LastD+(?<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>");
else
{
// GlobalProxySelection.Select = new DefaultControlObject("proxy", 80);
QuoteFetch q = new QuoteFetch(args[0]);
Console.WriteLine("{0} = {1}", args[0], q.Last);
}
}
}
Month: February 2011
My Web Client
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.Net;
using System.Text;
using System.IO;
namespace Client.Chapter_14___Networking_and_WWW
{
public class MyWebClient
{
[STAThread]
static void Main(string[] args)
{
WebClient MyClient = new WebClient();
Stream MyStream = MyClient.OpenRead("http://www.kutayzorlu.com/java2s/com.com");
StreamReader MyReader = new StreamReader(MyStream);
Console.WriteLine(MyReader.ReadLine());
MyStream.Close();
}
}
}
Displays the resource specified
using System;
using System.IO;
using System.Net;
public class TryURL {
public static void Main(String [] args) {
WebClient client = new WebClient();
client.BaseAddress = “http://www.kutayzorlu.com/java2s/com”;
client.DownloadFile(“www.kutayzorlu.com/java2s/com”, “index.htm”);
StreamReader input =new StreamReader(client.OpenRead(“index.htm”));
Console.WriteLine(input.ReadToEnd());
Console.WriteLine
(“Request header count: {0}”, client.Headers.Count);
WebHeaderCollection header = client.ResponseHeaders;
Console.WriteLine
(“Response header count: {0}”, header.Count);
for (int i = 0; i < header.Count; i++)
Console.WriteLine(" {0} : {1}",
header.GetKey(i), header[i]);
input.Close();
}
}
[/csharp]
Save web page from HttpWebResponse
using System;
using System.Net;
using System.IO;
public class WebApp {
public static void Main() {
String page = "http://www.yoursite.net/index.html";
HttpWebRequest site = (HttpWebRequest)WebRequest.Create(page);
HttpWebResponse response =(HttpWebResponse)site.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader read = new StreamReader(dataStream);
String data = read.ReadToEnd();
Console.WriteLine(data);
}
}
Basic WebClient
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
public class MainClass {
public static void Main() {
System.Net.WebClient Client = new WebClient();
Stream strm = Client.OpenRead("http://www.reuters.com");
StreamReader sr = new StreamReader(strm);
string line;
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
strm.Close();
}
}
Essentially creates a query string.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Web;
namespace Redwerb.BizArk.Core.Util
{
/// <summary>
/// Web related helper methods.
/// </summary>
public class WebUtil
{
/// <summary>
/// Essentially creates a query string.
/// </summary>
/// <param name="frmVals"></param>
/// <returns></returns>
public static string GetUrlEncodedData(NameValueCollection frmVals)
{
var sb = new StringBuilder();
foreach (string key in frmVals.AllKeys)
{
var value = frmVals[key];
if (sb.Length > 0) sb.Append("&");
sb.AppendFormat("{0}={1}", key, HttpUtility.UrlEncode(value));
}
return sb.ToString();
}
}
}
Correctly encode a name for a URL.
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers.
//CruiseControl is distributed under a BSD-style license.
//http://cruisecontrol.sourceforge.net/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ThoughtWorks.CruiseControl.Core.Util
{
/// <summary>
/// Class with handy stirng routines
/// </summary>
public class StringUtil
{
private static readonly Regex urlEncodeRegex = new Regex("[^a-zA-Z0-9.-_~]", RegexOptions.Compiled);
// public for testing only
public const string DEFAULT_DELIMITER = ",";
/// <summary>
/// Correctly encode a name for a URL.
/// </summary>
/// <param name="name">The name to encode.</param>
/// <returns>The encoded name.</returns>
/// <remarks>
/// <para>
/// HttpUtility.UrlEncode does not correctly encode for a URL, spaces get converted into
/// pluses, which can cause security errors.
/// </para>
/// <para>
/// This method will encode characters according to RFC 3986. This means only the following
/// characters are allowed un-encoded:
/// </para>
/// <para>
/// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s
/// t u v w x y z 0 1 2 3 4 5 6 7 8 9 - _ . ~
/// </para>
/// <para>
/// However, since the encoding only uses two-hex digits, it is not possible to encode non-ASCII
/// characters using this approach. Therefore we are using the RFC 3986 recommendation and assuming
/// the string will be using UTF-8 encoding and leaving the characters as they are.
/// </para>
/// </remarks>
public static string UrlEncodeName(string name)
{
var encodedName = urlEncodeRegex.Replace(name, (match) =>
{
var charValue = (int)match.Value[0];
var value = charValue >= 255 ? match.Value : "%" + string.Format("{0:x2}", charValue);
return value;
});
return encodedName;
}
}
}

