Csharp/CSharp Tutorial/Network/HttpWebRequest

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

Access the Internet

using System; 
using System.Net; 
using System.IO; 
 
class MainClass {  
  public static void Main() { 
    int ch; 
 
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.nfex.ru"); 
 
    HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); 
 
    Stream istrm = resp.GetResponseStream(); 
 
    for(int i=1; ; i++) { 
      ch =  istrm.ReadByte(); 
      if(ch == -1) 
         break; 
      Console.Write((char) ch); 
    } 
 
    resp.Close(); 
  } 
}
<HTML>
<HEAD>
     Java examples (example source code) Organized by topic </title>
...
...

Examine the headers

using System; 
using System.Net; 
 
class MainClass {  
  public static void Main() { 
 
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.nfex.ru"); 
 
    HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); 
 
    string[] names = resp.Headers.AllKeys; 
 
    Console.WriteLine("{0,-20}{1}\n", "Name", "Value"); 
    foreach(string n in names) 
      Console.WriteLine("{0,-20}{1}", n, resp.Headers[n]); 
 
    resp.Close(); 
  } 
}
Name                Value
Connection          close
Accept-Ranges       bytes
Content-Length      341387
Content-Type        text/html
Date                Sun, 25 Mar 2007 21:06:43 GMT
ETag                "14904bc-5358b-bce5400"
Last-Modified       Tue, 20 Feb 2007 15:25:04 GMT
Server              Apache/2.0.54 (Fedora)