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

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

WebClient.BaseAddress

<source lang="csharp">

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

}


 </source>


WebClient.Credentials

<source lang="csharp"> 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));
  }

}

 </source>


WebClient.DownloadData

<source lang="csharp"> 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));
  }

}

 </source>


WebClient.DownloadFile

<source lang="csharp"> 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."); 
 } 

}

 </source>


WebClient.DownloadString(Uri u)

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

}


 </source>


WebClient.Headers

<source lang="csharp"> 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();
 }

}


 </source>


WebClient.OpenRead(String url)

<source lang="csharp"> 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);
   }

}


 </source>


WebClient.OpenWrite

<source lang="csharp"> 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();
  }

}

 </source>


WebClient.ResponseHeaders

<source lang="csharp"> 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();
 }

}


 </source>


WebClient.UploadData

<source lang="csharp"> 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);
  }

}

 </source>


WebClient.UploadValues

<source lang="csharp">

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

}

 </source>