Csharp/CSharp Tutorial/Network/WebClient

Материал из .Net Framework эксперт
Версия от 15:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Download Data

<source lang="csharp">using System; using System.Net; using System.Text; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     byte[] response = wc.DownloadData("http://www.nfex.ru");
     Console.WriteLine(Encoding.ASCII.GetString(response));
  }

}</source>

Download File

<source lang="csharp">using System; using System.Net; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string filename = "index.html";
     wc.DownloadFile("http://www.nfex.ru", filename);
     Console.WriteLine("file downloaded");
  }

}</source>

Download HTM files

<source lang="csharp">using System; using System.IO; using System.Net; using System.Text.RegularExpressions; class MainClass {

   private static void Main() 
   {
       string remoteUri = "http://www.nfex.ru";
       
       WebClient client = new WebClient();
       string str = client.DownloadString(remoteUri);
       MatchCollection matches = Regex.Matches(str,@"http\S+[^-,;:?]\.htm");
       
       foreach(Match match in matches) 
       {
           foreach(Group grp in match.Groups) 
           {
               string file = grp.Value.Substring(grp.Value.LastIndexOf("/")+1);
               try
               {
                   Console.WriteLine("Downloading {0} to file {1}",
                       grp.Value, file);
                   //client.DownloadFile(new Uri(grp.Value), file);
               }
               catch
               {
                   Console.WriteLine("Failed to download {0}", grp.Value);
               }
           }
       }
   }

}</source>

Downloading http://www.nfex.ru/Code/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.nfex.ru/Tutorial/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.nfex.ru/Article/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.nfex.ru/Product/Java/CatalogJava.htm to file CatalogJava.htm

Download string from a website using the WebClient

<source lang="csharp">using System; using System.IO; using System.Net; using System.Text.RegularExpressions; class MainClass {

   private static void Main() 
   {
       string remoteUri = "http://www.nfex.ru";
       
       WebClient client = new WebClient();
       Console.WriteLine("Downloading {0}", remoteUri);
       string str = client.DownloadString(remoteUri);
       Console.WriteLine(str);
   }

}</source>

Downloading http://www.nfex.ru
<HTML>
<HEAD>
     Java examples (example source code) Organized by topic </title>
...
...

Open/Read Test

<source lang="csharp">using System; using System.IO; using System.Net; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string response;
     Stream strm = wc.OpenRead("http://www.nfex.ru");
     StreamReader sr = new StreamReader(strm);
     while(sr.Peek() > -1)
     {
        response = sr.ReadLine();
        Console.WriteLine(response);
     }
     sr.Close();
  }

}</source>

Open/Write Test

<source lang="csharp">using System; using System.IO; using System.Net; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string data = "Data up upload to server";
     Stream strm = wc.OpenWrite("http://www.nfex.ru");
     StreamWriter sw = new StreamWriter(strm);
     sw.WriteLine(data);
     sw.Close();
     strm.Close();
  }

}</source>

Response Headers

<source lang="csharp">using System; using System.Collections.Specialized; using System.Net; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     byte[] response = wc.DownloadData("http://www.jva2s.ru");
     WebHeaderCollection whc = wc.ResponseHeaders;
     Console.WriteLine("header count = {0}", whc.Count);
     for (int i = 0; i < whc.Count; i++)
     {
        Console.WriteLine(whc.GetKey(i) + " = " + whc.Get(i));
     }
  }

}</source>

Set download string for WebClient

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net;

  class Program
  {
     static void Main()
     {
        WebClient client = new WebClient();
        client.BaseAddress = "http://www.microsoft.ru";
        string data = client.DownloadString("Office");
        Console.WriteLine(data);
     }
  }</source>

Upload Data

<source lang="csharp">using System; using System.Net; using System.Text; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string data = "This is the data to post";
     byte[] dataarray = Encoding.ASCII.GetBytes(data);
     wc.UploadData("youraddress", dataarray);
  }

}</source>

Upload Values

<source lang="csharp">using System; using System.Collections.Specialized; using System.Net; using System.Text; class MainClass {

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string uri = "http://localhost/testform.aspx";
     NameValueCollection nvc = new NameValueCollection();
     nvc.Add("lastname", "B");
     nvc.Add("firstname", "A");
     byte[] response = wc.UploadValues(uri, nvc);
     Console.WriteLine(Encoding.ASCII.GetString(response));
  }

}</source>

Use WebClient to download information into a file

<source lang="csharp">using System; using System.Net; using System.IO;

class MainClass {

 public static void Main() { 
   WebClient user = new WebClient(); 
   string uri = "http://www.nfex.ru"; 
   string fname = "data.txt"; 
    
   try { 
     Console.WriteLine("Downloading data from " + uri + " to " + fname); 
     user.DownloadFile(uri, fname); 
   } catch (WebException exc) { 
     Console.WriteLine(exc); 
   } 

   Console.WriteLine("Download complete."); 
 } 

}</source>

Downloading data from http://www.nfex.ru to data.txt
Download complete.

Use WebClient to read website

<source lang="csharp">using System; using System.Net; using System.Text; using System.IO; class MainClass {

 [STAThread]
 static void Main(string[] args)
 {
   WebClient MyClient = new WebClient();
   Stream MyStream = MyClient.OpenRead("http://www.nfex.ru");
   StreamReader MyReader = new StreamReader(MyStream);
   Console.WriteLine(MyReader.ReadLine());
   MyStream.Close();
 }

}</source>

Web login

<source lang="csharp">using System; using System.Net; using System.Text; class MainClass {

  public static void Main()
  {
     WebClient wc = new WebClient();
     NetworkCredential nc = new NetworkCredential("usr", "mypassword");
     wc.Credentials = nc;
     byte[] response = wc.DownloadData("http://localhost/testlogin");
     Console.WriteLine(Encoding.ASCII.GetString(response));
  }

}</source>