文件地址:
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请大家提供一个有效的下载方法.
分给我下载测试通过的朋友
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请大家提供一个有效的下载方法.
分给我下载测试通过的朋友
自己顶
[/Quote
/// <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();
}
}
}
测试结果为:
long totalBytes = myrp.ContentLength;
其中totalBytes =0;
// 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();
}
这个数据源是一个不支持断点续传的服务器,它不向客户端报告文件长度。而 WebClient 这个组件和3楼的程序都依赖于服务器报告的文件长度。假如服务器不报告长度,就认为文件长度是0,根本不启动数据接收过程。对于这样的数据源,不能用 WebClient 这个组件。我知道的办法就是自己开 Socket,自己发送 HTTP 头,自己接收完所有数据,自己对接收到的数据进行解析,去掉HTTP头,仅将数据存盘。这样做太麻烦了。不知道 .net 里有没有不依赖服务器报告的文件长度的下载组件。
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";
wget "http://www.byfunds.com/ibm/uploadFile/downLoad.jsp?articleID=100002466&fileName=宝盈增强收益债券型证券投资基金更新招募说明书摘要201005.doc" -o "c:\a.doc"就能下载这个文件
{
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");
}
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
这个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);也失败
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的方式下载呢?
用IE不行吗?