以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  C#快餐-10  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=11724)


--  作者:admin
--  发布时间:11/9/2004 2:25:00 AM

--  C#快餐-10


发信人: nice (春天), 信区: DotNET        
标  题: C#快餐-10
发信站: BBS 水木清华站 (Thu Jul 26 02:13:25 2001)

Lesson 10. Simple Internet Programming

    The internet programming is very complex. There are many different  
conflicting protocols and the older technology conflicts with  a newer one.  
The idea of .Net is to make the internet more streamlined. Streamlining  
requires a uniform standard of web publishing. This standard would require  
web sites (servers) to provide extensive services for the users (clients).  
The internet then would become something like a huge operating system.  
I think that Microsoft has a very good idea and that the development of .Net  
will make the Internet more functional. However, I would like to separate  
technical issues from PR campaign since the latter tend to get out of control.  
The time will show whether or not Microsoft will succeed in making the  
Internet a better (and more profitable for Microsoft) place. I will start with  
a simple program that translates Domain Name into IP address.

  System.Net namespace is an interface for internet protocols which are  
currently supported by .Net. System.Net contains classes Dns, IPAddress,
IPHostEntry. Let me start with the class Dns. It provides simple domain name
resolution. Some of Dns 's methods are:

GetHostByAddr(hostname_string ) Creates an IPHostEntry instance from an IP  
dotted address. For example hostname_string=111.222.32

GetHostByName(hostname_string) Retrieves the IPHostEntry information  
corresponding to the Domain name provided in the host parameter.

GetHostName(void ) Gets the host name of the local machine.
InetAddr(ip_address_string) Converts an IP address string to an IPAddress  
instance.

Resolve(hostname_string) Translates a hostname to an IPAddress instance.

      The program uses method DNS.GetHostByName to create an IPHostEntry.
Methods of IPAddress has not yet been published by Microsoft at the time of  
this writing. For the purposes of this lesson we will  treat IPHostEntry as  
a black box.


using System;
using System.Net;
class IP
{
    IPAddress [] address;
    public void resolve (string host )
   {
      IPHostEntry entry = Dns.GetHostByName (host);
      address = entry.AddressList;
   }
    public void show_address()
   {
       for( int i = 0; i < address.Length; i++)
      Console.Write (address [i]);
   }
}
class Test
{
   public static void Main()
   {
      IP test = new IP ();
      string input = Console.ReadLine ();
      test.resolve (input);
      test.show_address ();
   }
}

Here is another simple program. The program treats a web page as a string of
text and reads it character by character until it encounters the end of  
page marker. Domain names need to be provided on command line in a complete  
form. For example to access ABC you would type http://www.abc.com/.The  
WebRequest and WebResponse are base classes  used to acces data from the  
Internet.  

The C# application works with instances of the WebRequest and WebResponse.  
The details of the request/response interaction  are carried out by protocol
-specific descendent classes. WebRequest.GetResponse() returns the response  
to the Internet request. WebRequestFactory creates WebRequestobjects for spe
cific network request schemes. The  program bellow also  illustrates usage  
of Input/Output library provided by System.Net namespace

using System;
using System.Net;
using System.IO;
public class Test
{
   public static void Main ()
   {
      string url_string = Console.ReadLine ();
      //make WebRequest to abc's URL
      WebRequest myRequest = WebRequest.Create (url_string);
      //store the response in myResponse
      WebResponse myResponse = myRequest.GetResponse();
      //register I/O stream associated with myResponse
      Stream myStream = myResponse.GetResponseStream ();
      //create StreamReader that reads characters one at a time
      StreamReader myReader = new StreamReader (myStream);
      string s = myReader.ReadToEnd ();
      Console.WriteLine (s); //keep reading till the end of the stream
   }
}

   There is absolutely no error checking in this program. I have left out  
exception handling for two reasons. First, to keep the program as simple  
as possible. Second, to illustrate huge differences between implementations  
of different websites. Web sites like http://xxx.lanl.govdeny this type of  
access.  

While http://www.cbs.comgenerates a large number of (JavaScript?) errors.

--

※ 修改:·walts 於 Jul 26 10:26:38 修改本文·[FROM: 166.111.142.118]
※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.176.234]
上一篇
返回上一页
回到目录
回到页首
下一篇



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms