Download Data Test

image_pdfimage_print
   


using System;
using System.Net;
using System.Text;

public class DownloadDataTest
{
   public static void Main(string[] argv)
   {
      WebClient wc = new WebClient();
      byte[] response = wc.DownloadData(argv[0]);
      Console.WriteLine(Encoding.ASCII.GetString(response));
   }
}


           
          


NetworkCredential test

image_pdfimage_print
   



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));
   }
}

           
          


NetworkCredential Cache Test

image_pdfimage_print
   

/*
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

image_pdfimage_print
   

/*
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>&amp;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

image_pdfimage_print
   

/*
 * 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

image_pdfimage_print

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

image_pdfimage_print
   


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);
    }
}