文件地址:
http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc
我写的程序为:
       string url = @"http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc";
        private void button2_Click(object sender, EventArgs e)
        {
            WebClient web = new WebClient();
            web.DownloadFile(url, "C:\\A.doc");
        }
但是下载下来的大小为0请大家提供一个有效的下载方法.
分给我下载测试通过的朋友

解决方案 »

  1.   

    [Quote=引用 1 楼 bobui 的回复:]
    自己顶
    [/Quote
      

  2.   


            /// <summary>        
            /// c#,.net 下载文件        
            /// </summary>        
            /// <param name="URL">下载文件地址</param>       
            /// 
            /// <param name="Filename">下载后的存放地址</param>        
            /// <param name="Prog">用于显示的进度条</param>        
            /// 
            public void DownloadFileRun(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)
            {
                float percent = 0;
                try
                {
                    Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                    myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                    long totalBytes = myrp.ContentLength;
                    if (prog != null)
                    {
                        prog.Maximum = (int)totalBytes;
                    }
                    st = myrp.GetResponseStream();
                    so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    long totalDownloadedByte = 0;
                    System.Windows.Forms.Application.DoEvents();
                    byte[] by = new byte[1024];
                    int osize = st.Read(by, 0, (int)by.Length);
                    while (osize > 0)
                    {
                        totalDownloadedByte = osize + totalDownloadedByte;
                        System.Windows.Forms.Application.DoEvents();
                        so.Write(by, 0, osize);
                        if (prog != null)
                        {
                            prog.Value = (int)totalDownloadedByte;
                        }
                        osize = st.Read(by, 0, (int)by.Length);
                        percent = ((float)totalDownloadedByte / (float)totalBytes) * 100;
                        label1.Text = ((int)percent).ToString() + "%";
                        System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
                    }
                    if (so != null)
                    {
                        so.Close();
                    }
                    if (st != null)
                    {
                        st.Close();
                    }
                }
                catch (System.Exception e)
                {
                        if (so != null)
                        {
                            so.Close();
                        }
                        if (st != null)
                        {
                            st.Close();
                        }
                    
                }
            }
      

  3.   

    谢谢上面的这位朋友,这个方法很好,但是针对这个数据源无法下载该数据
    测试结果为:
    long totalBytes = myrp.ContentLength;
    其中totalBytes =0;
      

  4.   


            // Methods
            public static void ExportDataSet(Control control, string filename)
            {
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = Encoding.Default;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                control.Page.EnableViewState = false;
                StringWriter writer = new StringWriter();
                HtmlTextWriter writer2 = new HtmlTextWriter(writer);
                control.RenderControl(writer2);
                HttpContext.Current.Response.Write(writer.ToString());
                HttpContext.Current.Response.End();
            }
      

  5.   

    不好意思,我这个winform的,修改了你的代码还是不行
      

  6.   

    用webhttprequest去请求你要下载的那个地址,就可以response回来你要的数据,具体的东西希望lz自己实现,全都给你了没任何好处
      

  7.   

    哦写反了 不好意思  应该是HttpWebRequest,它是WebRequest的一个子类
      

  8.   

    对url进行urlecode了,没用使用post方法,把“articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc”作为参数传递,没用把参数 urlecode,没用估计是文件名的问题,请楼主提供一个英文文件名或者没有空格的文件名让俺尝试下……
      

  9.   

    原因是这样的:
        这个数据源是一个不支持断点续传的服务器,它不向客户端报告文件长度。而 WebClient 这个组件和3楼的程序都依赖于服务器报告的文件长度。假如服务器不报告长度,就认为文件长度是0,根本不启动数据接收过程。对于这样的数据源,不能用 WebClient 这个组件。我知道的办法就是自己开 Socket,自己发送 HTTP 头,自己接收完所有数据,自己对接收到的数据进行解析,去掉HTTP头,仅将数据存盘。这样做太麻烦了。不知道 .net 里有没有不依赖服务器报告的文件长度的下载组件。
      

  10.   

    URL编码就可以了
    string url = @"http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=%b1%a6%d3%af%d4%f6%c7%bf%ca%d5%d2%e6%d5%ae%c8%af%d0%cd%d6%a4%c8%af%cd%b6%d7%ca%bb%f9%bd%f0%b8%fc%d0%c2%d5%d0%c4%bc%cb%b5%c3%f7%ca%e9%d5%aa%d2%aa201005.doc";
      

  11.   

    例如:在网上 wget.exe在你的程序中运行外部命令 
     wget "http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc" -o "c:\a.doc"就能下载这个文件
      

  12.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            WebClient w = new WebClient();
            w.DownloadFile("http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=%b1%a6%d3%af%d4%f6%c7%bf%ca%d5%d2%e6%d5%ae%c8%af%d0%cd%d6%a4%c8%af%cd%b6%d7%ca%bb%f9%bd%f0%b8%fc%d0%c2%d5%d0%c4%bc%cb%b5%c3%f7%ca%e9%d5%aa%d2%aa201005.doc", @"E:\tem\1");
        }
      

  13.   

    这中方式可行,但是在winform中,如何能使
    http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc
    编码成为
    http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=%b1%a6%d3%af%d4%f6%c7%bf%ca%d5%d2%e6%d5%ae%c8%af%d0%cd%d6%a4%c8%af%cd%b6%d7%ca%bb%f9%bd%f0%b8%fc%d0%c2%d5%d0%c4%bc%cb%b5%c3%f7%ca%e9%d5%aa%d2%aa201005.doc
      

  14.   

    string url = @"http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=%b1%a6%d3%af%d4%f6%c7%bf%ca%d5%d2%e6%d5%ae%c8%af%d0%cd%d6%a4%c8%af%cd%b6%d7%ca%bb%f9%bd%f0%b8%fc%d0%c2%d5%d0%c4%bc%cb%b5%c3%f7%ca%e9%d5%aa%d2%aa201005.doc";
    这个URL编码在winform怎么实现?
    我采用了下面两种方法均失败:
    方法一:(转十六进制)
     public static string UrlEncode(string str)
            {
                StringBuilder sb = new StringBuilder();
                byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
                for (int i = 0; i < byStr.Length; i++)
                {
                    sb.Append(@"%" + Convert.ToString(byStr[i], 16));
                }
                
                return (sb.ToString());
            }
    方法二
     string tmpurl0 = System.Web.HttpUtility.UrlEncode(url);另外 System.Web.HttpContext.Current.Server.UrlEncode(url);也失败
      

  15.   


    protected string UrlEncode2(string url)
            {
                byte[] bs = Encoding.GetEncoding("GB2312").GetBytes(url);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < bs.Length; i++)
                {
                    if (bs[i] < 128)
                        sb.Append((char)bs[i]);
                    else
                    {
                        sb.Append("%" + bs[i++].ToString("x").PadLeft(2, '0'));
                        sb.Append("%" + bs[i].ToString("x").PadLeft(2, '0'));
                    }
                }
                return sb.ToString();
            }这种方式可行大家能不能谈谈用TcpClient,或者WebRequest的方式下载呢?
      

  16.   

    我用的webclien可以下载。上传了资源。下载QQ通过。
      

  17.   

    System.Web.HttpUtility.UrlEncode("宝盈增强收益债券型证券投资基金更新招募说明书摘要", System.Text.Encoding.GetEncoding("GB2312"))
      

  18.   

    wget 应该可以的。
    用IE不行吗?
      

  19.   

    dou shi gao shou !