SpiderForm.cs
上传用户:yxdanqu
上传日期:2010-01-07
资源大小:84k
文件大小:12k
源码类别:

搜索引擎

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. namespace Spider
  8. {
  9. /// <summary>
  10. /// Provide a form to test the spider with.
  11. /// 
  12. /// This spider is copyright 2003 by Jeff Heaton. However, it is
  13. /// released under a Limited GNU Public License (LGPL). You may 
  14. /// use it freely in your own programs. For the latest version visit
  15. /// http://www.jeffheaton.com.
  16. /// </summary>
  17. public class SpiderForm : System.Windows.Forms.Form
  18. {
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.TextBox targetURL;
  22. private System.Windows.Forms.TextBox threadCount;
  23. private System.Windows.Forms.Button begin;
  24. private System.Windows.Forms.Label label3;
  25. private System.Windows.Forms.Panel panel1;
  26. private System.Windows.Forms.Label currentURL;
  27. private System.Windows.Forms.Label label5;
  28. private System.Windows.Forms.Label label6;
  29. private System.Windows.Forms.Label processedURLs;
  30. private System.Windows.Forms.Label elapsed;
  31. private System.Windows.Forms.Label label4;
  32. private System.Windows.Forms.TextBox outputDir;
  33. private Spider m_spider;
  34. /// <summary>
  35. /// Required designer variable.
  36. /// </summary>
  37. private System.ComponentModel.Container components = null;
  38. public SpiderForm()
  39. {
  40. //
  41. // Required for Windows Form Designer support
  42. //
  43. InitializeComponent();
  44. //
  45. // TODO: Add any constructor code after InitializeComponent call
  46. //
  47. }
  48. /// <summary>
  49. /// Clean up any resources being used.
  50. /// </summary>
  51. protected override void Dispose( bool disposing )
  52. {
  53. if( disposing )
  54. {
  55. if(components != null)
  56. {
  57. components.Dispose();
  58. }
  59. }
  60. base.Dispose( disposing );
  61. }
  62. #region Windows Form Designer generated code
  63. /// <summary>
  64. /// Required method for Designer support - do not modify
  65. /// the contents of this method with the code editor.
  66. /// </summary>
  67. private void InitializeComponent()
  68. {
  69. this.label1 = new System.Windows.Forms.Label();
  70. this.label2 = new System.Windows.Forms.Label();
  71. this.targetURL = new System.Windows.Forms.TextBox();
  72. this.threadCount = new System.Windows.Forms.TextBox();
  73. this.begin = new System.Windows.Forms.Button();
  74. this.label3 = new System.Windows.Forms.Label();
  75. this.panel1 = new System.Windows.Forms.Panel();
  76. this.elapsed = new System.Windows.Forms.Label();
  77. this.processedURLs = new System.Windows.Forms.Label();
  78. this.label6 = new System.Windows.Forms.Label();
  79. this.label5 = new System.Windows.Forms.Label();
  80. this.currentURL = new System.Windows.Forms.Label();
  81. this.outputDir = new System.Windows.Forms.TextBox();
  82. this.label4 = new System.Windows.Forms.Label();
  83. this.panel1.SuspendLayout();
  84. this.SuspendLayout();
  85. // 
  86. // label1
  87. // 
  88. this.label1.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  89. this.label1.Location = new System.Drawing.Point(8, 9);
  90. this.label1.Name = "label1";
  91. this.label1.Size = new System.Drawing.Size(120, 24);
  92. this.label1.TabIndex = 0;
  93. this.label1.Text = "要下载的URL:";
  94. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  95. // 
  96. // label2
  97. // 
  98. this.label2.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  99. this.label2.Location = new System.Drawing.Point(8, 52);
  100. this.label2.Name = "label2";
  101. this.label2.Size = new System.Drawing.Size(120, 24);
  102. this.label2.TabIndex = 1;
  103. this.label2.Text = "线程数量:";
  104. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  105. // 
  106. // targetURL
  107. // 
  108. this.targetURL.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  109. this.targetURL.Location = new System.Drawing.Point(136, 9);
  110. this.targetURL.Name = "targetURL";
  111. this.targetURL.Size = new System.Drawing.Size(264, 22);
  112. this.targetURL.TabIndex = 2;
  113. this.targetURL.Text = "http://www.wwf.org.hk/chi/index.html";
  114. // 
  115. // threadCount
  116. // 
  117. this.threadCount.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  118. this.threadCount.Location = new System.Drawing.Point(136, 43);
  119. this.threadCount.Name = "threadCount";
  120. this.threadCount.Size = new System.Drawing.Size(264, 22);
  121. this.threadCount.TabIndex = 3;
  122. this.threadCount.Text = "20";
  123. // 
  124. // begin
  125. // 
  126. this.begin.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  127. this.begin.Location = new System.Drawing.Point(224, 112);
  128. this.begin.Name = "begin";
  129. this.begin.Size = new System.Drawing.Size(90, 25);
  130. this.begin.TabIndex = 4;
  131. this.begin.Text = "Begin";
  132. this.begin.Click += new System.EventHandler(this.begin_Click);
  133. // 
  134. // label3
  135. // 
  136. this.label3.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  137. this.label3.Location = new System.Drawing.Point(16, 9);
  138. this.label3.Name = "label3";
  139. this.label3.Size = new System.Drawing.Size(112, 17);
  140. this.label3.TabIndex = 5;
  141. this.label3.Text = "当前处理的URL:";
  142. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  143. // 
  144. // panel1
  145. // 
  146. this.panel1.Controls.Add(this.elapsed);
  147. this.panel1.Controls.Add(this.processedURLs);
  148. this.panel1.Controls.Add(this.label6);
  149. this.panel1.Controls.Add(this.label5);
  150. this.panel1.Controls.Add(this.currentURL);
  151. this.panel1.Controls.Add(this.label3);
  152. this.panel1.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  153. this.panel1.Location = new System.Drawing.Point(8, 146);
  154. this.panel1.Name = "panel1";
  155. this.panel1.Size = new System.Drawing.Size(400, 130);
  156. this.panel1.TabIndex = 6;
  157. // 
  158. // elapsed
  159. // 
  160. this.elapsed.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  161. this.elapsed.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  162. this.elapsed.Location = new System.Drawing.Point(144, 86);
  163. this.elapsed.Name = "elapsed";
  164. this.elapsed.Size = new System.Drawing.Size(248, 17);
  165. this.elapsed.TabIndex = 10;
  166. this.elapsed.Text = "...";
  167. // 
  168. // processedURLs
  169. // 
  170. this.processedURLs.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  171. this.processedURLs.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  172. this.processedURLs.Location = new System.Drawing.Point(144, 52);
  173. this.processedURLs.Name = "processedURLs";
  174. this.processedURLs.Size = new System.Drawing.Size(248, 17);
  175. this.processedURLs.TabIndex = 9;
  176. this.processedURLs.Text = "...";
  177. // 
  178. // label6
  179. // 
  180. this.label6.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  181. this.label6.Location = new System.Drawing.Point(10, 86);
  182. this.label6.Name = "label6";
  183. this.label6.Size = new System.Drawing.Size(124, 17);
  184. this.label6.TabIndex = 8;
  185. this.label6.Text = "已用时间:";
  186. this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  187. // 
  188. // label5
  189. // 
  190. this.label5.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  191. this.label5.Location = new System.Drawing.Point(10, 47);
  192. this.label5.Name = "label5";
  193. this.label5.Size = new System.Drawing.Size(124, 18);
  194. this.label5.TabIndex = 7;
  195. this.label5.Text = "已处理的URL:";
  196. this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  197. // 
  198. // currentURL
  199. // 
  200. this.currentURL.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  201. this.currentURL.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  202. this.currentURL.Location = new System.Drawing.Point(144, 9);
  203. this.currentURL.Name = "currentURL";
  204. this.currentURL.Size = new System.Drawing.Size(248, 17);
  205. this.currentURL.TabIndex = 6;
  206. this.currentURL.Text = "...";
  207. // 
  208. // outputDir
  209. // 
  210. this.outputDir.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  211. this.outputDir.Location = new System.Drawing.Point(136, 78);
  212. this.outputDir.Name = "outputDir";
  213. this.outputDir.Size = new System.Drawing.Size(264, 22);
  214. this.outputDir.TabIndex = 8;
  215. this.outputDir.Text = "c:\temp\";
  216. // 
  217. // label4
  218. // 
  219. this.label4.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  220. this.label4.Location = new System.Drawing.Point(8, 86);
  221. this.label4.Name = "label4";
  222. this.label4.Size = new System.Drawing.Size(120, 25);
  223. this.label4.TabIndex = 7;
  224. this.label4.Text = "输出目录:";
  225. this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  226. // 
  227. // SpiderForm
  228. // 
  229. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  230. this.ClientSize = new System.Drawing.Size(416, 290);
  231. this.Controls.Add(this.outputDir);
  232. this.Controls.Add(this.threadCount);
  233. this.Controls.Add(this.targetURL);
  234. this.Controls.Add(this.label4);
  235. this.Controls.Add(this.panel1);
  236. this.Controls.Add(this.begin);
  237. this.Controls.Add(this.label2);
  238. this.Controls.Add(this.label1);
  239. this.Name = "SpiderForm";
  240. this.Text = "Internet蜘蛛 1.0";
  241. this.Closing += new System.ComponentModel.CancelEventHandler(this.SpiderForm_Close);
  242. this.Load += new System.EventHandler(this.SpiderForm_Load);
  243. this.panel1.ResumeLayout(false);
  244. this.ResumeLayout(false);
  245. }
  246. #endregion
  247. private void begin_Click(object sender, System.EventArgs e)
  248. {
  249. ThreadStart starter = new ThreadStart(this.SpiderThread);
  250. Thread spider = new Thread(starter);
  251. spider.Start();
  252. }
  253. public void SpiderThread()
  254. {
  255. if( begin.Text.Equals("Cancel") )
  256. {
  257. m_spider.Quit = true;
  258. begin.Enabled = false;
  259. }
  260. else
  261. {
  262. begin.Text = "Cancel";
  263. targetURL.Enabled = false;
  264. threadCount.Enabled = false;
  265. outputDir.Enabled = false;
  266. m_spider = new Spider();
  267. m_spider.ReportTo = this;
  268. m_spider.OutputPath = outputDir.Text;
  269. int threads = int.Parse( threadCount.Text);
  270. if(threads<1)
  271. threads = 1;
  272. threadCount.Text = ""+threads;
  273. try
  274. {
  275. m_spider.Start(new Uri(this.targetURL.Text),threads);
  276. }
  277. catch( UriFormatException ex)
  278. {
  279. System.Windows.Forms.MessageBox.Show( ex.Message );
  280. return;
  281. }
  282. begin.Text = "Begin";
  283. targetURL.Enabled = true;
  284. threadCount.Enabled = true;
  285. outputDir.Enabled = true;
  286. begin.Enabled = true;
  287. }
  288. }
  289. public void SetLastURL(string str)
  290. {
  291. currentURL.Text = str;
  292. }
  293. public void SetElapsedTime(string str)
  294. {
  295. elapsed.Text = str;
  296. }
  297. public void SetProcessedCount(string str)
  298. {
  299. processedURLs.Text = str;
  300. }
  301. private void SpiderForm_Close(object sender, CancelEventArgs e)
  302. {
  303. Application.Exit();
  304. }
  305. private void SpiderForm_Load(object sender, System.EventArgs e)
  306. {
  307. }
  308. }
  309. }