Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 7k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net.Sockets;
  8. using System.Net;
  9. using System.IO;
  10. namespace ch7_5
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.TextBox textBox1;
  19. private System.Windows.Forms.TextBox textBox2;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.Button button1;
  22. private System.Windows.Forms.Button button2;
  23. /// <summary>
  24. /// 必需的设计器变量。
  25. /// </summary>
  26. private System.ComponentModel.Container components = null;
  27. public Form1()
  28. {
  29. //
  30. // Windows 窗体设计器支持所必需的
  31. //
  32. InitializeComponent();
  33. //
  34. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  35. //
  36. }
  37. /// <summary>
  38. /// 清理所有正在使用的资源。
  39. /// </summary>
  40. protected override void Dispose( bool disposing )
  41. {
  42. if( disposing )
  43. {
  44. if (components != null) 
  45. {
  46. components.Dispose();
  47. }
  48. }
  49. base.Dispose( disposing );
  50. }
  51. #region Windows Form Designer generated code
  52. /// <summary>
  53. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  54. /// 此方法的内容。
  55. /// </summary>
  56. private void InitializeComponent()
  57. {
  58. this.label1 = new System.Windows.Forms.Label();
  59. this.textBox1 = new System.Windows.Forms.TextBox();
  60. this.textBox2 = new System.Windows.Forms.TextBox();
  61. this.label2 = new System.Windows.Forms.Label();
  62. this.button1 = new System.Windows.Forms.Button();
  63. this.button2 = new System.Windows.Forms.Button();
  64. this.SuspendLayout();
  65. // 
  66. // label1
  67. // 
  68. this.label1.Location = new System.Drawing.Point(24, 16);
  69. this.label1.Name = "label1";
  70. this.label1.Size = new System.Drawing.Size(72, 16);
  71. this.label1.TabIndex = 0;
  72. this.label1.Text = "请输入网址";
  73. // 
  74. // textBox1
  75. // 
  76. this.textBox1.Location = new System.Drawing.Point(24, 40);
  77. this.textBox1.Name = "textBox1";
  78. this.textBox1.Size = new System.Drawing.Size(224, 21);
  79. this.textBox1.TabIndex = 1;
  80. this.textBox1.Text = "";
  81. // 
  82. // textBox2
  83. // 
  84. this.textBox2.Location = new System.Drawing.Point(24, 120);
  85. this.textBox2.Name = "textBox2";
  86. this.textBox2.Size = new System.Drawing.Size(224, 21);
  87. this.textBox2.TabIndex = 2;
  88. this.textBox2.Text = "";
  89. // 
  90. // label2
  91. // 
  92. this.label2.Location = new System.Drawing.Point(24, 80);
  93. this.label2.Name = "label2";
  94. this.label2.TabIndex = 3;
  95. this.label2.Text = "选择保存文件";
  96. // 
  97. // button1
  98. // 
  99. this.button1.Location = new System.Drawing.Point(176, 80);
  100. this.button1.Name = "button1";
  101. this.button1.TabIndex = 4;
  102. this.button1.Text = "浏览";
  103. this.button1.Click += new System.EventHandler(this.button1_Click);
  104. // 
  105. // button2
  106. // 
  107. this.button2.Location = new System.Drawing.Point(176, 168);
  108. this.button2.Name = "button2";
  109. this.button2.TabIndex = 5;
  110. this.button2.Text = "下载";
  111. this.button2.Click += new System.EventHandler(this.button2_Click);
  112. // 
  113. // Form1
  114. // 
  115. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  116. this.ClientSize = new System.Drawing.Size(280, 221);
  117. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  118.   this.button2,
  119.   this.button1,
  120.   this.label2,
  121.   this.textBox2,
  122.   this.textBox1,
  123.   this.label1});
  124. this.Name = "Form1";
  125. this.Text = "网页下载";
  126. this.ResumeLayout(false);
  127. }
  128. #endregion
  129. /// <summary>
  130. /// 应用程序的主入口点。
  131. /// </summary>
  132. [STAThread]
  133. static void Main() 
  134. {
  135. Application.Run(new Form1());
  136. }
  137. private void button1_Click(object sender, System.EventArgs e)
  138. {
  139. SaveFileDialog sDialog=new SaveFileDialog();
  140. sDialog.Filter="网页(*.htm;*.html)|*.htm;*.html";
  141.             //显示保存文件对话框
  142. if (sDialog.ShowDialog()==DialogResult.OK)
  143. {
  144.             //得到保存文件名
  145. this.textBox2.Text=sDialog.FileName;
  146. }
  147. }
  148. private void button2_Click(object sender, System.EventArgs e)
  149. {
  150. if (this.textBox2.Text!="")
  151. {
  152. //得到保存文件名
  153. string fileName=this.textBox2.Text.Trim();
  154. //得到运程主机名
  155. string hostName=this.textBox1.Text.Trim();
  156. //得到主机信息 
  157. IPHostEntry ipInfo=Dns.GetHostByName(hostName); 
  158. //取得IPAddress地址列表
  159. IPAddress[] ipAddr=ipInfo.AddressList; 
  160. //得到ip 
  161. IPAddress ip=ipAddr[0]; 
  162. //组合出远程终结点 
  163. IPEndPoint hostEP=new IPEndPoint(ip,80);
  164. //创建Socket 实例 
  165. Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 
  166. try 
  167. //尝试连接 
  168. socket.Connect(hostEP); 
  169. catch(Exception se) 
  170. MessageBox.Show("连接错误"+se.Message,"提示信息"
  171. ,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information); 
  172. //发送给远程主机的请求内容串 
  173. string sendStr="GET / HTTP/1.1rnHost: " + hostName + 
  174. "rnConnection: Closernrn"; 
  175. //创建bytes字节数组以转换发送串 
  176. byte[] bytesSendStr=new byte[1024]; 
  177. //将发送内容字符串转换成字节byte数组 
  178. bytesSendStr=System.Text.Encoding.Default.GetBytes(sendStr); 
  179. try 
  180. //向主机发送请求 
  181.      socket.Send(bytesSendStr,bytesSendStr.Length,0); 
  182. catch(Exception ce) 
  183. MessageBox.Show("发送错误:"+ce.Message,"提示信息 "
  184. ,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information); 
  185. //声明接收返回内容的字符串 
  186. string receiveStr=""; 
  187. //声明字节数组,一次接收数据的长度为1024字节 
  188. byte[] receiveBytes=new byte[1024]; 
  189. //返回实际接收内容的字节数 
  190. int bytes=0; 
  191. //循环读取,直到接收完所有数据 
  192. while(true) 
  193. bytes=socket.Receive(receiveBytes,receiveBytes.Length,0); 
  194. //读取完成后退出循环 
  195. if(bytes<=0) 
  196. break; 
  197. //将读取的字节数转换为字符串 
  198. receiveStr+=System.Text.Encoding.Default.GetString(receiveBytes,0,bytes); 
  199. //将所读取的字符串转换为字节数组 
  200. string s=receiveStr.Substring(receiveStr.IndexOf("<"));
  201. byte[] content=System.Text.Encoding.Default.GetBytes(s); 
  202. try 
  203. //创建文件流对象实例 
  204. FileStream fs=new FileStream(fileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); 
  205. //写入文件 
  206. fs.Write(content,0,content.Length); 
  207.                     //保存成功提示
  208. MessageBox.Show("保存网页成功","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information); 
  209. catch(Exception fe) 
  210. MessageBox.Show("文件创建/写入错误:"+fe.Message,"提示信息",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information); 
  211. }
  212.   
  213. //禁用Socket 
  214. socket.Shutdown(SocketShutdown.Both); 
  215. //关闭Socket 
  216. socket.Close(); 
  217. }
  218. else
  219. {
  220. MessageBox.Show("请输入网址","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
  221. }
  222. }
  223. }
  224. }