Csharp/C Sharp/Network/Web Client

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

Access the Internet

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Access the Internet.

using System; using System.Net; using System.IO;

public class NetDemo {

 public static void Main() { 
   int ch; 

   // First, create a WebRequest to a URI. 
   HttpWebRequest req = (HttpWebRequest) 
          WebRequest.Create("http://www.nfex.ru"); 

   // Next, send that request and return the response. 
   HttpWebResponse resp = (HttpWebResponse) 
          req.GetResponse(); 

   // From the response, obtain an input stream. 
   Stream istrm = resp.GetResponseStream(); 


   /* Now, read and display the html present at 
      the specified URI.  So you can see what is 
      being displayed, the data is shown 
      400 characters at a time.  After each 400 
      characters are displayed, you must press 
      ENTER to get the next 400. */ 
  
   for(int i=1; ; i++) { 
     ch =  istrm.ReadByte(); 
     if(ch == -1) break; 
     Console.Write((char) ch); 
     if((i%400)==0) { 
       Console.Write("\nPress a key."); 
       Console.Read(); 
     } 
   } 

   // Close the Response. This also closes istrm. 
   resp.Close(); 
 } 

}


      </source>


Basic WebClient

<source lang="csharp">

using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Net; public class MainClass {

   public static void Main() {
       System.Net.WebClient Client = new WebClient();
       Stream strm = Client.OpenRead("http://www.reuters.ru");
       StreamReader sr = new StreamReader(strm);
       string line;
       while ((line = sr.ReadLine()) != null) {
           Console.WriteLine(line);
       }
       strm.Close();
   }

}

      </source>


Displays the resource specified

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


Download Data Test

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

}


      </source>


Download File Test

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

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     string filename = "webpage.htm";
     wc.DownloadFile(argv[0], filename);
     Console.WriteLine("file downloaded");
  }

}


      </source>


Examine the headers

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Examine the headers.

using System; using System.Net;

public class HeaderDemo {

 public static void Main() { 

   // Create a WebRequest to a URI. 
   HttpWebRequest req = (HttpWebRequest) 
          WebRequest.Create("http://www.osborne.ru"); 

   // Send that request and return the response. 
   HttpWebResponse resp = (HttpWebResponse) 
          req.GetResponse(); 

   // Obtain a list of the names. 
   string[] names = resp.Headers.AllKeys; 

   // Display the header name/value pairs. 
   Console.WriteLine("{0,-20}{1}\n", "Name", "Value"); 
   foreach(string n in names) 
     Console.WriteLine("{0,-20}{1}", n, resp.Headers[n]); 

   // Close the Response.  
   resp.Close(); 
 } 

}


      </source>


Handle network exceptions

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Handle network exceptions.

using System; using System.Net; using System.IO;

public class NetExcDemo {

 public static void Main() { 
   int ch; 

   try { 

     // First, create a WebRequest to a URI. 
     HttpWebRequest req = (HttpWebRequest) 
            WebRequest.Create("http://www.osborne.ru"); 

     // Next, send that request and return the response. 
     HttpWebResponse resp = (HttpWebResponse) 
            req.GetResponse(); 

     // From the response, obtain an input stream. 
     Stream istrm = resp.GetResponseStream(); 

     /* Now, read and display the html present at 
        the specified URI.  So you can see what is 
        being displayed, the data is shown 
        400 characters at a time.  After each 400 
        characters are displayed, you must press 
        ENTER to get the next 400. */ 
  
     for(int i=1; ; i++) { 
       ch =  istrm.ReadByte(); 
       if(ch == -1) break; 
       Console.Write((char) ch); 
       if((i%400)==0) { 
         Console.Write("\nPress a key."); 
         Console.Read(); 
       } 
     } 


     // Close the Response. This also closes istrm. 
     resp.Close(); 

   } catch(WebException exc) { 
     Console.WriteLine("Network Error: " + exc.Message +  
                       "\nStatus code: " + exc.Status); 
   } catch(ProtocolViolationException exc) { 
     Console.WriteLine("Protocol Error: " + exc.Message); 
   } catch(UriFormatException exc) { 
     Console.WriteLine("URI Format Error: " + exc.Message); 
   } catch(NotSupportedException exc) { 
     Console.WriteLine("Unknown Protocol: " + exc.Message); 
   } catch(IOException exc) { 
     Console.WriteLine("I/O Error: " + exc.Message); 
   } 
 } 

}


      </source>


My Web Client

<source lang="csharp"> /*

* 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.nfex.ru.ru");
     StreamReader MyReader = new StreamReader(MyStream);
     Console.WriteLine(MyReader.ReadLine());
     MyStream.Close();
   }
 }

}

      </source>


NetworkCredential Cache Test

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

}


      </source>


NetworkCredential test

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


Reading Web Pages

<source lang="csharp"> /* A Programmer"s Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3

  • /

// 32 - .NET Frameworks Overview\Reading 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.ru/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> 78 3/16"
       
       Regex lastmatch = new Regex(@"Last\D+(?<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);
       }
   }

}

      </source>


Save web page from HttpWebResponse

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

}

      </source>


Use LastModified

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Use LastModified.

using System; using System.Net;

public class HeaderDemo12 {

 public static void Main() { 

   HttpWebRequest req = (HttpWebRequest) 
          WebRequest.Create("http://www.Microsoft.ru"); 

   HttpWebResponse resp = (HttpWebResponse) 
          req.GetResponse(); 

   Console.WriteLine("Last modified: " + resp.LastModified); 

   resp.Close(); 
 } 

}


      </source>


Use WebClient to download information into a file

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Use WebClient to download information into a file.

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>


Web Client Open Read Test

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

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

}

      </source>


Web Client Open Write Test

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


Web Client Response Headers Test

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

  public static void Main(string[] argv)
  {
     WebClient wc = new WebClient();
     byte[] response = wc.DownloadData(argv[0]);
     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>


Web Client Upload Data Test 2

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


Web Client Upload Values Test

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


Web Get

<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765

  • /

using System; using System.Drawing; using System.IO; using System.Net; using System.Windows.Forms; public class WebGet : Form {

  private TextBox uribox;
  private ListBox headers;
  private ListBox cookies;
  private ListBox response;
  public WebGet()
  {
     Text = "WebGet - a web page retriever";
     Size = new Size(500, 450);
     Label label1 = new Label();
     label1.Parent = this;
     label1.Text = "URI:";
     label1.AutoSize = true;
     label1.Location = new Point(10, 23);
     uribox = new TextBox();
     uribox.Parent = this;
     uribox.Size = new Size(200, 2 * Font.Height);
     uribox.Location = new Point(35, 20);
     Label label2 = new Label();
     label2.Parent = this;
     label2.Text = "Headers:";
     label2.AutoSize = true;
     label2.Location = new Point(10, 46);
     headers = new ListBox();
     headers.Parent = this;
     headers.HorizontalScrollbar = true;
     headers.Location = new Point(10, 65);
     headers.Size = new Size(450, 6 * Font.Height);
     Label label3 = new Label();
     label3.Parent = this;
     label3.Text = "Cookies:";
     label3.AutoSize = true;
     label3.Location = new Point(10, 70 + 6 * Font.Height);
     cookies = new ListBox();
     cookies.Parent = this;
     cookies.HorizontalScrollbar = true;
     cookies.Location = new Point(10, 70 + 7 * Font.Height);
     cookies.Size = new Size(450, 6 * Font.Height);
     Label label4 = new Label();
     label4.Parent = this;
     label4.Text = "HTML:";
     label4.AutoSize = true;
     label4.Location = new Point(10, 70 + 13 * Font.Height);
     response = new ListBox();
     response.Parent = this;
     response.HorizontalScrollbar = true;
     response.Location = new Point(10, 70 + 14 * Font.Height);
     response.Size = new Size(450, 12 * Font.Height);
     Button sendit = new Button();
     sendit.Parent = this;
     sendit.Text = "GetIt";
     sendit.Location = new Point(275, 18);
     sendit.Size = new Size(7 * Font.Height, 2 * Font.Height);
     sendit.Click += new EventHandler(ButtongetitOnClick);
  }
  void ButtongetitOnClick(object obj, EventArgs ea)
  {
     headers.Items.Clear();
     cookies.Items.Clear();
     response.Items.Clear();
     HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(uribox.Text);
     hwr.CookieContainer = new CookieContainer();
     HttpWebResponse hwrsp = (HttpWebResponse)hwr.GetResponse();
     WebHeaderCollection whc = hwrsp.Headers;
     for (int i = 0; i < whc.Count; i++)
     {
        headers.Items.Add(whc.GetKey(i) + " = " + whc.Get(i));
     }
     hwrsp.Cookies = hwr.CookieContainer.GetCookies(hwr.RequestUri);
     foreach(Cookie cky in hwrsp.Cookies)
     {
        cookies.Items.Add(cky.Name + " = " + cky.Value);
     }
     Stream strm = hwrsp.GetResponseStream();
     StreamReader sr = new StreamReader(strm);
     while (sr.Peek() > -1)
     {
        response.Items.Add(sr.ReadLine());
     }
     sr.Close();
     strm.Close();
  }
  public static void Main()
  {
     Application.Run(new WebGet());
  }

}

      </source>