Csharp/CSharp Tutorial/Network/WebClient

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Download Data

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

Download File

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

Download HTM files

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

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);
    }
}
Downloading http://www.nfex.ru
<HTML>
<HEAD>
     Java examples (example source code) Organized by topic </title>
...
...

Open/Read Test

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

Open/Write Test

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

Response Headers

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

Set download string for WebClient

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

Upload Data

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

Upload Values

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

Use WebClient to download information into a file

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."); 
  } 
}
Downloading data from http://www.nfex.ru to data.txt
Download complete.

Use WebClient to read website

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

Web login

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