using System; using System.Net; using System.Text; public class CredTest { public static void Main() { WebClient wc = new WebClient(); NetworkCredential nc = new NetworkCredential("alex", "mypassword"); wc.Credentials = nc; byte[] response = wc.DownloadData("http://www.kutayzorlu.com/java2s/com/index.htm"); Console.WriteLine(Encoding.ASCII.GetString(response)); } }
Author: coder
NetworkCredential Cache Test
/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Net; using System.Text; public class CredCacheTest { public static void Main() { WebClient wc = new WebClient(); string website1 = "http://remote1.ispnet.net"; string website2 = "http://remote2.ispnet.net"; string website3 = "http://remote3.ispnet.net/login"; NetworkCredential nc1 = new NetworkCredential("mike", "guitars"); NetworkCredential nc2 = new NetworkCredential("evonne", "singing", "home"); NetworkCredential nc3 = new NetworkCredential("alex", "drums"); CredentialCache cc = new CredentialCache(); cc.Add(new Uri(website1), "Basic", nc1); cc.Add(new Uri(website2), "Basic", nc2); cc.Add(new Uri(website3), "Digest", nc3); wc.Credentials = cc; wc.DownloadFile(website1, "website1.htm"); wc.DownloadFile(website2, "website2.htm"); wc.DownloadFile(website3, "website3.htm"); } }
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 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); } } }
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(); } }