Form1.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:2k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- namespace UseWebClient
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnGet_Click(object sender, EventArgs e)
- {
- try
- {
- WebClient client = new WebClient();
- //client.BaseAddress = "shaozhd";
- Stream response = client.OpenRead(tbURL.Text);
- tbContent.Text = "";
- int nByteData;
- do
- {
- nByteData = response.ReadByte();
- if (nByteData > 0)
- tbContent.Text += Convert.ToChar(nByteData);
- } while (nByteData > 0);
- response.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- private void btnDownload_Click(object sender, EventArgs e)
- {
- try
- {
- WebClient client = new WebClient();
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- string strDes = dlg.SelectedPath + Path.GetFileName(tbAddress.Text);
- client.DownloadFile(tbAddress.Text, strDes);
- MessageBox.Show("文件下载到:"+ strDes) ;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
- }
- }