Csharp/C Sharp by API/System.Net/WebClient

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

WebClient.BaseAddress

 

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.nfex.ru";
    client.DownloadFile("www.nfex.ru", "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();
  }
}


WebClient.Credentials

 
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.nfex.ru/index.htm");
      Console.WriteLine(Encoding.ASCII.GetString(response));
   }
}


WebClient.DownloadData

 
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.nfex.ru/index.htm");
      Console.WriteLine(Encoding.ASCII.GetString(response));
   }
}


WebClient.DownloadFile

 
using System; 
using System.Net; 
using System.IO; 
 
public class WebClientDemo {  
  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); 
    } catch (UriFormatException exc) { 
      Console.WriteLine(exc); 
    } 
      
    Console.WriteLine("Download complete."); 
  } 
}


WebClient.DownloadString(Uri u)

   
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);
        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);
                Console.WriteLine(file);
            }
        }
    }
}


WebClient.Headers

 
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.nfex.ru";
    client.DownloadFile("www.nfex.ru", "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();
  }
}


WebClient.OpenRead(String url)

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
class MainClass
{
    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        Stream rssFeedStream = client.OpenRead("http://yourRssFeedURL");
        XmlReader reader = XmlReader.Create(rssFeedStream);
        reader.MoveToContent();
        while (reader.ReadToFollowing("item"))
        {
            ProcessItem(reader.ReadSubtree());
        }
    }
    static void ProcessItem(XmlReader reader)
    {
        reader.ReadToFollowing("title");
        string title = reader.ReadElementContentAsString("title", reader.NamespaceURI);
        reader.ReadToFollowing("link");
        string link = reader.ReadElementContentAsString("link", reader.NamespaceURI);
        Console.WriteLine("{0}\n\t{1}", title, link);
    }
}


WebClient.OpenWrite

 
using System;
using System.IO;
using System.Net;
public class OpenWriteTest
{
   public static void Main(string[] argv)
   {
      WebClient wc = new WebClient();
      string data = "Data up upload to server";
      Stream strm = wc.OpenWrite(argv[0]);
      StreamWriter sw = new StreamWriter(strm);
      sw.WriteLine(data);
      sw.Close();
      strm.Close();
   }
}


WebClient.ResponseHeaders

 
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.nfex.ru";
    client.DownloadFile("www.nfex.ru", "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();
  }
}


WebClient.UploadData

 
using System;
using System.Net;
using System.Text;
public class UploadDataTest
{
   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(argv[0], dataarray);
   }
}


WebClient.UploadValues

 

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
public class UploadValuesTest
{
   public static void Main(string[] argv)
   {
      WebClient wc = new WebClient();
      string uri = "http://localhost/testform.aspx";
      NameValueCollection nvc = new NameValueCollection();
      nvc.Add("lastname", "Blum");
      nvc.Add("firstname", "Rich");
      byte[] response = wc.UploadValues(uri, nvc);
      Console.WriteLine(Encoding.ASCII.GetString(response));
   }
}