home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / RemTools / httpget.cs next >
Encoding:
Text File  |  2000-06-23  |  3.0 KB  |  113 lines

  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.IO;
  5. using System.Globalization;
  6.  
  7. class HttpGet
  8. {
  9.     static void GetPage(String url)
  10.     {
  11.         int                     BufferSize = 1024;
  12.         WebRequest              Request;
  13.         WebResponse             Response;
  14.         Stream                  RespStream;
  15.         byte []                 Buffer;
  16.         int                     BytesRead;
  17.  
  18.         /* -- uncommment this code and replace "httpproxy" with the name of your
  19.            -- http proxy server
  20.            
  21.         String defaultProxyName = "httpproxy";
  22.         int defaultProxyPort = 80;
  23.  
  24.         // create a new Object with the Proxy Information that 
  25.         //  you wish to use
  26.  
  27.         DefaultControlObject proxyObject = new DefaultControlObject(defaultProxyName, defaultProxyPort); 
  28.  
  29.         // Disable Proxy use when the host is local i.e. without periods.
  30.         proxyObject.ProxyNoLocal = true;
  31.  
  32.         // Now actually take over the global with our new settings, all new requests 
  33.         // use this proxy info
  34.  
  35.         GlobalProxySelection.Select = proxyObject;
  36.         */
  37.  
  38.         Request = WebRequestFactory.Create(url);
  39.  
  40.         Response = Request.GetResponse();
  41.  
  42.         Console.WriteLine("Status code: " + Response.Status.ToString());
  43.  
  44.         RespStream = Response.GetResponseStream();
  45.  
  46.         BufferSize = (int)Response.ContentLength;
  47.  
  48.         Console.WriteLine("Entity body:");
  49.  
  50.         Buffer = new byte[BufferSize];
  51.  
  52.         BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  53.  
  54.         while (BytesRead != 0)
  55.         {
  56.             String content = Encoding.ASCII.GetString(Buffer, 0, BytesRead); 
  57.             Console.Write(content);
  58.  
  59.             BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  60.  
  61.         }
  62.     }
  63.  
  64.     public static String url;
  65.  
  66.     public static void  Main(string[] Args)
  67.     {
  68.         int ret = Initialize(Args);
  69.  
  70.         if (0 == ret)
  71.          GetPage(url);
  72.     }
  73.  
  74.     public static int Initialize(String[] args)
  75.     {
  76.         if (args.Length == 0)
  77.         {
  78.             Usage();
  79.             return -1;
  80.         }
  81.  
  82.  
  83.         for (int i=0;i<args.Length;i++)
  84.         {
  85.             if (
  86.                String.Compare(args[i],"HELP", true) == 0 ||
  87.                String.Compare(args[i],"?", true) == 0 ||
  88.                String.Compare(args[i],"/h", true) == 0 ||
  89.                String.Compare(args[i],"-h", true) == 0 ||
  90.                String.Compare(args[i],"-?", true) == 0 ||
  91.                String.Compare(args[i],"/?", true) == 0
  92.                )
  93.             {
  94.                 Usage();
  95.                 return -1;
  96.             }
  97.  
  98.             if (args[i].CompareTo("-url")==0)
  99.             {
  100.                 url = args[i+1];
  101.             }
  102.         }
  103.  
  104.         return 0;
  105.     }
  106.  
  107.     public static void Usage()
  108.     {
  109.         Console.WriteLine("Usage: httpget [-url http://...]\n");
  110.         Console.WriteLine("Examples : httpget -url http://localhost/Trader/StockQuote.soap");
  111.     }
  112. }
  113.