Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 6k
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_6
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.TextBox textBox1;
  18. private System.Windows.Forms.TextBox textBox2;
  19. private System.Windows.Forms.Button button1;
  20. private System.Windows.Forms.Button button2;
  21. private System.Windows.Forms.Label label1;
  22. private System.Windows.Forms.Label label2;
  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.textBox1 = new System.Windows.Forms.TextBox();
  59. this.textBox2 = new System.Windows.Forms.TextBox();
  60. this.button1 = new System.Windows.Forms.Button();
  61. this.button2 = new System.Windows.Forms.Button();
  62. this.label1 = new System.Windows.Forms.Label();
  63. this.label2 = new System.Windows.Forms.Label();
  64. this.SuspendLayout();
  65. // 
  66. // textBox1
  67. // 
  68. this.textBox1.Location = new System.Drawing.Point(32, 64);
  69. this.textBox1.Name = "textBox1";
  70. this.textBox1.Size = new System.Drawing.Size(208, 21);
  71. this.textBox1.TabIndex = 0;
  72. this.textBox1.Text = "trueice.dhs.org";
  73. // 
  74. // textBox2
  75. // 
  76. this.textBox2.Location = new System.Drawing.Point(32, 152);
  77. this.textBox2.Name = "textBox2";
  78. this.textBox2.Size = new System.Drawing.Size(200, 21);
  79. this.textBox2.TabIndex = 1;
  80. this.textBox2.Text = "";
  81. // 
  82. // button1
  83. // 
  84. this.button1.Location = new System.Drawing.Point(152, 104);
  85. this.button1.Name = "button1";
  86. this.button1.TabIndex = 2;
  87. this.button1.Text = "浏览";
  88. this.button1.Click += new System.EventHandler(this.button1_Click);
  89. // 
  90. // button2
  91. // 
  92. this.button2.Location = new System.Drawing.Point(152, 192);
  93. this.button2.Name = "button2";
  94. this.button2.TabIndex = 3;
  95. this.button2.Text = "保存";
  96. this.button2.Click += new System.EventHandler(this.button2_Click);
  97. // 
  98. // label1
  99. // 
  100. this.label1.Location = new System.Drawing.Point(32, 24);
  101. this.label1.Name = "label1";
  102. this.label1.Size = new System.Drawing.Size(80, 16);
  103. this.label1.TabIndex = 4;
  104. this.label1.Text = "请输入网址";
  105. // 
  106. // label2
  107. // 
  108. this.label2.Location = new System.Drawing.Point(32, 112);
  109. this.label2.Name = "label2";
  110. this.label2.Size = new System.Drawing.Size(100, 16);
  111. this.label2.TabIndex = 5;
  112. this.label2.Text = "选择保存文件";
  113. // 
  114. // Form1
  115. // 
  116. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  117. this.ClientSize = new System.Drawing.Size(272, 245);
  118. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  119.   this.label2,
  120.   this.label1,
  121.   this.button2,
  122.   this.button1,
  123.   this.textBox2,
  124.   this.textBox1});
  125. this.Name = "Form1";
  126. this.Text = "网页下载";
  127. this.ResumeLayout(false);
  128. }
  129. #endregion
  130. /// <summary>
  131. /// 应用程序的主入口点。
  132. /// </summary>
  133. [STAThread]
  134. static void Main() 
  135. {
  136. Application.Run(new Form1());
  137. }
  138. private void button1_Click(object sender, System.EventArgs e)
  139. {
  140. SaveFileDialog sDialog=new SaveFileDialog();
  141. sDialog.Filter="网页(*.htm;*.html)|*.htm;*.html";
  142. //显示保存文件对话框
  143. if (sDialog.ShowDialog()==DialogResult.OK)
  144. {
  145. //得到保存文件名
  146. this.textBox2.Text=sDialog.FileName;
  147. }
  148. }
  149. private void button2_Click(object sender, System.EventArgs e)
  150. {
  151. //得到保存文件名
  152. string fileName=this.textBox2.Text.Trim();
  153. //得到网页名称
  154. string url=this.textBox1.Text.Trim();
  155.    
  156. //如果前面没有加"http://"标志则加上。
  157. if (url.IndexOf(@"http://")==-1 )
  158. {
  159. url=@"http://"+url;
  160. }
  161. //构造Web请求
  162. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  163. //发送请求,获取响应
  164. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  165. //使用到的读写流
  166. Stream inStream = null;
  167. FileStream fileStream = null;
  168. try
  169. {
  170. //获得流
  171. inStream = response.GetResponseStream();
  172. //获得文件长度
  173. long fileSizeInBytes = response.ContentLength;
  174. //创建文件流对象
  175. fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
  176. //读取数据缓冲区长度和缓冲区
  177. int length = 1024;
  178. byte[] buffer = new byte[1025];
  179. //记录读取的长度
  180. int bytesread = 0;
  181. string s="";
  182. //从网络读取数据
  183. while((bytesread = inStream.Read(buffer, 0, length)) > 0)
  184. {
  185. //把数据写入文件
  186. fileStream.Write(buffer, 0, bytesread);
  187. s+=System.Text.Encoding.Default.GetString(buffer,0,bytesread);
  188. }
  189. }
  190. catch(Exception fe)
  191. {
  192. MessageBox.Show("文件创建/写入错误:"+fe.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information); 
  193. }
  194. finally
  195. {
  196. //关闭流
  197. if(inStream != null)
  198. {
  199. inStream.Close();
  200. }
  201. if(fileStream != null)
  202. {
  203. fileStream.Close();
  204. }
  205. }
  206. }
  207. }
  208. }