http://db.2ycn.com/1/11.rar
上面地址放入地址栏里面可以访问得到。
但是用程序访问老出现远程服务器返回错误: (400) 错误的请求。
不知道什么。发个例子给我,如果从网上抄的,最好试,我的代码就是从网上下的,用不了 using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net;
    using System.IO;
    using System.Threading;
    namespace download
    {
        /// <summary>
        /// Summary description for WinForm.
        /// </summary>
        public class WinForm : System.Windows.Forms.Form
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.ProgressBar progressBar1;
            public WinForm()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose(disposing);
            }
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
                this.label2 = new System.Windows.Forms.Label();
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.SuspendLayout();
                //
                // label1
                //
                this.label1.Location = new System.Drawing.Point(40, 40);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(40, 16);
                this.label1.TabIndex = 0;
                this.label1.Text = "URL:";
                //
                // textBox1
                //
                this.textBox1.Location = new System.Drawing.Point(72, 36);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(256, 21);
                this.textBox1.TabIndex = 1;
                this.textBox1.Text = "";
                //
                // button1
                //
                this.button1.Location = new System.Drawing.Point(256, 120);
                this.button1.Name = "button1";
                this.button1.TabIndex = 2;
                this.button1.Text = "下载";
                this.button1.Click += new System.EventHandler(this.button1_Click);
                //
                // label2
                //
                this.label2.Location = new System.Drawing.Point(8, 80);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(72, 23);
                this.label2.TabIndex = 3;
                this.label2.Text = "下载进度:";
                //
                // progressBar1
                //
                this.progressBar1.Location = new System.Drawing.Point(72, 80);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(256, 16);
                this.progressBar1.TabIndex = 4;
                //
                // WinForm
                //
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(360, 173);
                this.Controls.Add(this.progressBar1);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.label1);
                this.MaximizeBox = false;
                this.Name = "WinForm";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "文件下载";
                this.ResumeLayout(false);
            }
            #endregion
            //// <summary>
            //// The main entry point for the application.
            //// </summary>
            ////[STAThread]
            ////static void Main()
            ////{
            ////    Application.Run(new WinForm());
            ////}
            private void downfile()
            {
                string FileName;
                WebClient DownFile = new WebClient();
                long fbytes;
                if (textBox1.Text != "")
                {
                    //saveFileDialog1.ShowDialog();
                    FileName = "c:\\aaa";
                    if (FileName != "")
                    {
                        //取得文件大小
                        WebRequest wr_request = WebRequest.Create(textBox1.Text);
                        WebResponse wr_response = wr_request.GetResponse();//就是这一步报错
                        fbytes = wr_response.ContentLength;
                        progressBar1.Maximum = (int)fbytes;
                        progressBar1.Step = 1;
                        wr_response.Close();
                        //开始下载数据
                        DownFile.DownloadData(textBox1.Text);
                        Stream strm = DownFile.OpenRead(textBox1.Text);
                        StreamReader reader = new StreamReader(strm);
                        byte[] mbyte = new byte[fbytes];
                        int allmybyte = (int)mbyte.Length;
                        int startmbyte = 0;
                        while (fbytes > 0)
                        {
                            int m = strm.Read(mbyte, startmbyte, allmybyte);
                            if (m == 0) break;
                            startmbyte += m;
                            allmybyte -= m;
                            progressBar1.Value += m;
                        }
                        FileStream fstrm = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
                        fstrm.Write(mbyte, 0, startmbyte);
                        strm.Close();
                        fstrm.Close();
                        progressBar1.Value = progressBar1.Maximum;
                    }
                }
                else
                {
                    MessageBox.Show("没有输入要下载的文件!");
                }
            }
            private void button1_Click(object sender, System.EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(downfile));
                th.Start();
            }
        }
    }

解决方案 »

  1.   

    WebResponse wr_response = wr_request.GetResponse();//就是这一步报错
      

  2.   

    WebClient webClient = new WebClient();
    webClient.DownloadFile(URL,Path);
    http://topic.csdn.net/u/20090831/14/37bc33f1-4c84-4a11-b4d9-4b7e21acad4c.html
      

  3.   

    使用WEBCLIENT 下载文件:private void DownLoadFile()
    {
        try
        {
            WebClient client = new WebClient();
            if (!Directory.Exists(Application.StartupPath + @""update"))
            {
                Directory.CreateDirectory(Application.StartupPath + @""update");
            }
            for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
            {
                string fileName = this.listView1.Items[i].SubItems[0].Text;
                string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
                string file = Application.StartupPath + """update""" + fileName;
                this.label2.Text = "正在下载:" + fileName;            Stream myStream = client.OpenRead(url);
                byte[] bBuffer = new byte[10240];
                int nRealCount = 0;
                DateTime dtStart = DateTime.Now;
                TimeSpan ts;            int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
                int cur = 0;
                progressBar1.Value = 0;
                FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
                //do  
                //{                                                
                //    dtStart = DateTime.Now;  
                //    nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);  
                //    if(nRealCount > 0)  
                //    {  
                //        fs.Write(bBuffer, 0, nRealCount);  
                //    }  
                //    ts = DateTime.Now - dtStart;  
                //    cur = cur + nRealCount;  
                //    double pe = cur;  
                //    pe = pe * 100 / lenth;  
                //    int va = Convert.ToInt32(pe);
                //    progressBar1.Value = va;                                              
                //    this.label2.Text = "正在下载:" + fileName  + "       " + cur / 1024 + "   KB/" + lenth / 1024 + "   KB";  
                //    MessageBox.Show("" + ts.Milliseconds + "     " + ts.TotalMilliseconds);  
                //   //   this.label2.Text = "正在下载:" + fileName + "       " + cur /1024 + "   KB/" + lenth / 1024 + "   KB" + "     下载速度:"+((double )nRealCount/(int)(ts.TotalMilliseconds/1000))+"   kb/s";  
                //   //   计算流量可以用nRealCount / ts.Seconds来进行估算  
                //}  
                int value = 0;
                int counter = 1;
                do
                {
                    if (counter % 10 == 0)
                    {
                        ts = DateTime.Now - dtStart;                    int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024;                    this.label3.Text = "速度:" + perSecondsValue + "   k/s";
                        dtStart = DateTime.Now;
                        value = 0;
                     }                 nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
                     if (nRealCount > 0)
                     {
                         fs.Write(bBuffer, 0, nRealCount);
                     }
                     cur = cur + nRealCount;
                     double pe = cur;
                     pe = pe * 100 / lenth;
                     int va = Convert.ToInt32(pe);
                     progressBar1.Value = va;                 this.label2.Text = "正在下载:" + fileName + "  " + cur / 1024 + " KB/" + lenth / 1024 + " KB";                 value += nRealCount;
                     counter++;
                    }
                        while (nRealCount > 0);                    fs.Close();
                        //   client.DownloadFile(url, file);  
                        this.listView1.Items[i].SubItems.Add("已下载");
                 }             this.progressBar1.Visible = false;
                 this.label3.Visible = false;
                 this.label2.Text = "正在更新文件,请稍后...";
                 string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @""update");             foreach (string str in fileDir)
                 {
                     int pos = str.LastIndexOf(@""");
                     string FileName = str.Substring(pos);
                     string FilePath = Application.StartupPath + FileName;
                     File.Copy(str, FilePath, true);
                 }             this.label2.Text = "程序更新已完成!";             this.label1.Text = this.finish;
                 this.btnNext.Enabled = false;
                 this.listView1.Visible = false;
                 this.btnExit.Text = "完成(&F)";
                 step++;
         }
         catch (Exception errMsg)
         {
             MessageBox.Show(errMsg.Message);
         }
    }