using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;

namespace File_Downloader_and_Uploader
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainApp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tpUpload;
		private System.Windows.Forms.TabPage tpdownload;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox tbSaveAs;
		private System.Windows.Forms.SaveFileDialog saveFileDialog;

		private WebClient client=new WebClient();
		private System.Windows.Forms.Button btnDownload;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Button btnUpload;
		private System.Windows.Forms.Button btnDownloadExit;
		private System.Windows.Forms.Button btnDownloadBrowser;
		private System.Windows.Forms.TextBox tbDownloadURL;
		private System.Windows.Forms.TextBox tbUploadURL;
		private System.Windows.Forms.TextBox tbOpen;
		private System.Windows.Forms.Button btnUploadBrowser;
		private System.Windows.Forms.Button btnUploadExit;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox tbMethod;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainApp()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tpdownload = new System.Windows.Forms.TabPage();
			this.btnDownloadExit = new System.Windows.Forms.Button();
			this.btnDownload = new System.Windows.Forms.Button();
			this.btnDownloadBrowser = new System.Windows.Forms.Button();
			this.tbSaveAs = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.tbDownloadURL = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tpUpload = new System.Windows.Forms.TabPage();
			this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.tbUploadURL = new System.Windows.Forms.TextBox();
			this.tbOpen = new System.Windows.Forms.TextBox();
			this.btnUploadBrowser = new System.Windows.Forms.Button();
			this.btnUpload = new System.Windows.Forms.Button();
			this.btnUploadExit = new System.Windows.Forms.Button();
			this.label7 = new System.Windows.Forms.Label();
			this.tbMethod = new System.Windows.Forms.TextBox();
			this.tabControl1.SuspendLayout();
			this.tpdownload.SuspendLayout();
			this.tpUpload.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tpdownload,
																					  this.tpUpload});
			this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(488, 165);
			this.tabControl1.TabIndex = 0;
			// 
			// tpdownload
			// 
			this.tpdownload.Controls.AddRange(new System.Windows.Forms.Control[] {
																					 this.label5,
																					 this.btnDownloadExit,
																					 this.btnDownload,
																					 this.btnDownloadBrowser,
																					 this.tbSaveAs,
																					 this.label2,
																					 this.tbDownloadURL,
																					 this.label1});
			this.tpdownload.Location = new System.Drawing.Point(4, 21);
			this.tpdownload.Name = "tpdownload";
			this.tpdownload.Size = new System.Drawing.Size(480, 140);
			this.tpdownload.TabIndex = 1;
			this.tpdownload.Text = "下载";
			// 
			// btnDownloadExit
			// 
			this.btnDownloadExit.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnDownloadExit.Location = new System.Drawing.Point(352, 104);
			this.btnDownloadExit.Name = "btnDownloadExit";
			this.btnDownloadExit.Size = new System.Drawing.Size(64, 23);
			this.btnDownloadExit.TabIndex = 6;
			this.btnDownloadExit.Text = "退出";
			this.btnDownloadExit.Click += new System.EventHandler(this.btnDownloadExit_Click);
			// 
			// btnDownload
			// 
			this.btnDownload.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnDownload.Location = new System.Drawing.Point(272, 104);
			this.btnDownload.Name = "btnDownload";
			this.btnDownload.Size = new System.Drawing.Size(64, 24);
			this.btnDownload.TabIndex = 5;
			this.btnDownload.Text = "下载";
			this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click);
			// 
			// btnDownloadBrowser
			// 
			this.btnDownloadBrowser.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnDownloadBrowser.Location = new System.Drawing.Point(352, 64);
			this.btnDownloadBrowser.Name = "btnDownloadBrowser";
			this.btnDownloadBrowser.Size = new System.Drawing.Size(64, 24);
			this.btnDownloadBrowser.TabIndex = 4;
			this.btnDownloadBrowser.Text = "浏览";
			this.btnDownloadBrowser.Click += new System.EventHandler(this.btnBrowser_Click);
			// 
			// tbSaveAs
			// 
			this.tbSaveAs.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tbSaveAs.Location = new System.Drawing.Point(88, 64);
			this.tbSaveAs.Name = "tbSaveAs";
			this.tbSaveAs.Size = new System.Drawing.Size(248, 22);
			this.tbSaveAs.TabIndex = 3;
			this.tbSaveAs.Text = "";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "保存为:";
			// 
			// tbDownloadURL
			// 
			this.tbDownloadURL.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tbDownloadURL.Location = new System.Drawing.Point(88, 24);
			this.tbDownloadURL.Name = "tbDownloadURL";
			this.tbDownloadURL.Size = new System.Drawing.Size(248, 22);
			this.tbDownloadURL.TabIndex = 1;
			this.tbDownloadURL.Text = "http://";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "网址:";
			// 
			// tpUpload
			// 
			this.tpUpload.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.tbMethod,
																				   this.label7,
																				   this.btnUploadExit,
																				   this.btnUpload,
																				   this.btnUploadBrowser,
																				   this.tbOpen,
																				   this.tbUploadURL,
																				   this.label6,
																				   this.label4,
																				   this.label3});
			this.tpUpload.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tpUpload.Location = new System.Drawing.Point(4, 21);
			this.tpUpload.Name = "tpUpload";
			this.tpUpload.Size = new System.Drawing.Size(480, 140);
			this.tpUpload.TabIndex = 0;
			this.tpUpload.Text = "上载";
			// 
			// saveFileDialog
			// 
			this.saveFileDialog.FileName = "doc1";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(16, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 23);
			this.label3.TabIndex = 0;
			this.label3.Text = "网址:";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(16, 64);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 23);
			this.label4.TabIndex = 1;
			this.label4.Text = "打开:";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(16, 112);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(184, 23);
			this.label5.TabIndex = 7;
			this.label5.Text = "从指定网址下载文件到本机";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(16, 96);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(248, 40);
			this.label6.TabIndex = 2;
			this.label6.Text = "使用指定的方法将指定的本地文件上载到指定的资源。";
			// 
			// tbUploadURL
			// 
			this.tbUploadURL.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tbUploadURL.Location = new System.Drawing.Point(88, 24);
			this.tbUploadURL.Name = "tbUploadURL";
			this.tbUploadURL.Size = new System.Drawing.Size(224, 22);
			this.tbUploadURL.TabIndex = 3;
			this.tbUploadURL.Text = "http://";
			// 
			// tbOpen
			// 
			this.tbOpen.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tbOpen.Location = new System.Drawing.Point(88, 64);
			this.tbOpen.Name = "tbOpen";
			this.tbOpen.Size = new System.Drawing.Size(224, 22);
			this.tbOpen.TabIndex = 4;
			this.tbOpen.Text = "";
			// 
			// btnUploadBrowser
			// 
			this.btnUploadBrowser.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnUploadBrowser.Location = new System.Drawing.Point(400, 64);
			this.btnUploadBrowser.Name = "btnUploadBrowser";
			this.btnUploadBrowser.Size = new System.Drawing.Size(64, 23);
			this.btnUploadBrowser.TabIndex = 5;
			this.btnUploadBrowser.Text = "浏览";
			this.btnUploadBrowser.Click += new System.EventHandler(this.btnUploadBrowser_Click);
			// 
			// btnUpload
			// 
			this.btnUpload.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnUpload.Location = new System.Drawing.Point(288, 104);
			this.btnUpload.Name = "btnUpload";
			this.btnUpload.TabIndex = 6;
			this.btnUpload.Text = "上载";
			this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
			// 
			// btnUploadExit
			// 
			this.btnUploadExit.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnUploadExit.Location = new System.Drawing.Point(392, 104);
			this.btnUploadExit.Name = "btnUploadExit";
			this.btnUploadExit.TabIndex = 7;
			this.btnUploadExit.Text = "退出";
			this.btnUploadExit.Click += new System.EventHandler(this.btnUploadExit_Click);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(320, 24);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(40, 23);
			this.label7.TabIndex = 8;
			this.label7.Text = "方法:";
			// 
			// tbMethod
			// 
			this.tbMethod.Location = new System.Drawing.Point(368, 24);
			this.tbMethod.Name = "tbMethod";
			this.tbMethod.TabIndex = 10;
			this.tbMethod.Text = "";
			// 
			// MainApp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(488, 165);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tabControl1});
			this.Name = "MainApp";
			this.Text = "文件上载/下载器";
			this.tabControl1.ResumeLayout(false);
			this.tpdownload.ResumeLayout(false);
			this.tpUpload.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnBrowser_Click(object sender, System.EventArgs e)
		{
			if	(saveFileDialog.ShowDialog()==DialogResult.OK)
			{
				tbSaveAs.Text=saveFileDialog.FileName;
			}
			else
			{
				MessageBox.Show("没有选择文件","Warning");
			}
		}

		private void btnDownload_Click(object sender, System.EventArgs e)
		{
			string URL=tbDownloadURL.Text ;
			this.Text="正在下载......";
			client.DownloadFile(URL , tbSaveAs.Text);
			this.Text="文件上载/下载器";
		}

		private void btnUpload_Click(object sender, System.EventArgs e)
		{
			byte[] responseArray=null;
			try
			{
				responseArray = client.UploadFile(tbUploadURL.Text,tbMethod.Text ,tbOpen.Text);
			}
			catch(Exception ex)
			{}
			finally
			{
				MessageBox.Show("\n服务器的回复为: \n"+System.Text.Encoding.ASCII.GetString(responseArray));
			}
		}

		private void btnUploadBrowser_Click(object sender, System.EventArgs e)
		{
			if	(openFileDialog.ShowDialog()==DialogResult.OK)
			{
				tbOpen.Text=openFileDialog.FileName;
			}
			else
			{
				MessageBox.Show("没有选择文件","Warning");
			}
		}

		private void btnDownloadExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void btnUploadExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		public static void Main()
		{
			Application.Run(new MainApp());
		}
	}
}
