精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>asp.net教程>正文

构造.NET环境下的网页下载器 (2)


来源:站长站 时间:06-09-10 点击: 点击这里收藏本文
// 
   this.Filename.Location = new System. 
   Drawing.Point(96, 64); 
   this.Filename.Name = "Filename"; 
   this.Filename.Size = new System. 
   Drawing.Size(176, 21); 
   this.Filename.TabIndex = 4; 
   this.Filename.Text = ""; 
   // 
   // Form1 
   // 
   this.AutoScaleBaseSize = new System. 
   Drawing.Size(6, 14); 
   this.ClientSize = new System.Drawing. 
   Size(376, 117); 
   this.Controls.AddRange(new System.Windows. 
   Forms.Control[] { 
   this.Filename, 
   this.ServerAddress, 
   this.Download, 
   this.label2, 
   this.label1}); 
   this.Name = "Form1"; 
   this.Text = "网页下载器"; 
   this.ResumeLayout(false); 
   } 
   #endregion 
   
   ///  
   /// 应用程序的主入口点。 
   /// 
 
   [STAThread] 
   static void Main() 
   { 
   Application.Run(new Form1()); 
   } 
   
   private string DoSocketGet(string server) 
   { 
   //定义一些必要的变量以及一条要发送到服务器的字符串 
   Encoding ASCII = Encoding.ASCII; 
   string Get = "GET / HTTP/1.1\r\nHost: " 
   +server+"\r\nConnection: Close\r\n\r\n"; 
   Byte[] ByteGet = ASCII.GetBytes(Get); 
   Byte[] RecvBytes = new Byte[256]; 
   String strRetPage = null; 
   
   //获取服务器相关的IP地址列表,其中第一项即为我们所需的 
   IPAddress hostadd = Dns.Resolve(server). 
   AddressList[0]; 
   
   //根据获得的服务器的IP地址创建一个终结点,端口为默认的80 
   IPEndPoint EPhost = new IPEndPoint 
   (hostadd, 80); 
   
   //创建一个Socket实例 
   Socket s = new Socket(AddressFamily. 
   InterNetwork, SocketType.Stream, 
   ProtocolType.Tcp ); 
   try 
   { 
   //用上面所取得的终结点连接到服务器 
   s.Connect(EPhost); 
   } 
   catch(Exception se) 
   { 
   MessageBox.Show("连接错误:"+se. 
   Message,"提示信息",MessageBoxButtons. 
   RetryCancel,MessageBoxIcon.Information); 
   } 
   
   if (!s.Connected) 
   { 
   strRetPage = "不能连接到服务器!"; 
   return strRetPage; 
   } 
   
   try 
   { 
   //向服务器发送GET命令 
   s.Send(ByteGet, ByteGet.Length, 
   SocketFlags.None); 
   } 
   catch(Exception ce) 
   { 
   MessageBox.Show("发送错误:"+ce. 
   Message,"提示信息",MessageBoxButtons. 
   RetryCancel,MessageBoxIcon.Information); 
   } 
   
   //接收页面数据,直到所有字节接收完毕 
   Int32 bytes = s.Receive(RecvBytes, 
   RecvBytes.Length, 0); 
   strRetPage = "以下是在服务器" + server + 
   "上的默认网页:\r\n"; 
   strRetPage = strRetPage + ASCII.GetString 
   (RecvBytes, 0, bytes); 
   
   while (bytes > 0) 
   { 
   bytes = s.Receive(RecvBytes, 
   RecvBytes.Length, SocketFlags.None); 
   strRetPage = strRetPage + ASCII. 
   GetString(RecvBytes, 0, bytes); 
   } 
   
   //禁用并关闭Socket实例 
   s.Shutdown(SocketShutdown.Both); 
   s.Close(); 
   return strRetPage; 
   } 
   
   private void Download_Click(object sender, System. 
   EventArgs e) 
   { 
   //将所读取的字符串转换为字节数组 
   byte[] content=Encoding.ASCII.GetBytes 
   (DoSocketGet(ServerAddress.Text)); 
   try 
   { 
   //创建文件流对象实例 
   FileStream fs=new FileStream 
   (Filename.Text,FileMode.OpenOrCreate,FileAccess. 
   ReadWrite); 
   //写入文件 
   fs.Write(content,0,content.Length); 
   } 
   catch(Exception fe) 
   { 
   MessageBox.Show("文件创建/写入错误: 
   "+fe.Message,"提示信息",MessageBoxButtons. 
   RetryCancel,MessageBoxIcon.Information); 
   } 
   } 
   } 
  } 
   
   
   
  其中主要的函数为DoSocketGet(),首先程序在响应"开始下载"的事件处理函数Download_Click(),调用DoSocketGet()函数,该函数完成了套接字的创建、连接、与主机的通讯-即获得主机上的网页、禁用、关闭等功能。在调用完DoSocketGet()函数后,Download_Click()函数创建一个FileStream对象,并试图将DoSocketGet()函数返回的网页文件以字节数组的形式写到本机文件中,最终形成在本机上的一个Html文件,这样就完成了一个网页文件的下载工作了。 
   
    不过这个程序的功能比较简单,不能作为真正的网页浏览器用,网页文件下载后还是要用IE等浏览器才能打开。然而作为一个解释.NET底层网络编程的实例绝对是一个好例子,所以希望读者能好好研究,同时读者还可以添加文件下载进度条等以完善本程序。 
   
    注:以上程序在Windows 2000服务器版、Visual Studio.Net中文正式版下调试通过 


9 7 3 1 2 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.