关于C#下载网络文件的问题

该隐叶 2013-10-28 11:13:28
本人目前要坐一个自动更新程序,就用到了后台下载。用的如下方法有两个,都可以实现功能

/// <summary>
/// 根据Web地址下载网络附件
/// </summary>
/// <param name="webUrl">Web文件路径</param>
/// <param name="localPath">存放本地路径</param>
public void DownWebFile(string webUrl, string localPath)
{
#region 下载方法一
///下载方法一
System.Net.WebClient wc = new System.Net.WebClient();
wc.Headers["User-Agent"] = "blah";
byte[] bateFile = wc.DownloadData(new Uri(webUrl));
System.IO.FileStream fs = new System.IO.FileStream(localPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(bateFile, 0, bateFile.Length);
fs.Flush();
fs.Close();
#endregion

#region 下载方法二
///下载方法二
//System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(webUrl);
//System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
//System.IO.Stream st = myrp.GetResponseStream();
//System.IO.Stream so = new System.IO.FileStream(localPath, System.IO.FileMode.Create);
//byte[] by = new byte[1024];
//int osize = st.Read(by, 0, (int)by.Length);
//while (osize > 0)
//{
// so.Write(by, 0, osize);
// osize = st.Read(by, 0, (int)by.Length);
//}
//so.Close();
//st.Close();
//myrp.Close();
//Myrq.Abort();
#endregion
}



我测试的时候,是在本地IIS里面部署了一个网站,放进去了几个文件,其中有个文件夹名字是 bin ,但是在win7里面下载bin目录的时候就会提示404错误,在windows2003环境下就不会提示这个错误。路径绝对是正确的,我给bin改成bin1就可以下载了。

还有一个奇怪的,在windows2003环境下,如果下载的文件名有.exe后缀名也会提示404错误,不知道各位有没有碰到过,下面是我传递的路径:

win7环境下:
可以下载的路径:
http://localhost:81/Files/bin1/test1.zip
提示404错误的路径:
http://localhost:81/Files/bin/test1.zip



2003环境下:
可以下载的路径:
http://localhost:81/Files/testApp.exe
提示404错误的路径:
http://localhost:81/Files/test.zip(目前测试大部分的后缀名文件都可以就是exe不行)


注:两个路径都是我手动改的,文件也肯定存在两个目录下面。


...全文
272 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卧_槽 2013-10-28
  • 打赏
  • 举报
回复
直接用浏览器可以下载么?
该隐叶 2013-10-28
  • 打赏
  • 举报
回复
这个写反了! 2003环境下: 提示404错误的路径: http://localhost:81/Files/testApp.exe 可以下载的路径: http://localhost:81/Files/test.zip(目前测试大部分的后缀名文件都可以就是exe不行)
该隐叶 2013-10-28
  • 打赏
  • 举报
回复
win7环境下: 可以下载的路径: http://localhost:81/Files/bin1/test1.zip 提示404错误的路径: http://localhost:81/Files/bin/test1.zip 2003环境下: 可以下载的路径: http://localhost:81/Files/testApp.exe 提示404错误的路径: http://localhost:81/Files/test.zip(目前测试大部分的后缀名文件都可以就是exe不行) 注:两个路径都是我手动改的,文件也肯定存在两个目录下面。
卧_槽 2013-10-28
  • 打赏
  • 举报
回复
引用 4 楼 u011129454 的回复:
[quote=引用 3 楼 yuwenge 的回复:] 直接用浏览器可以下载么?
用IE浏览器不行,用其他浏览器可以,用迅雷等软件也可以[/quote] ie报什么错?
该隐叶 2013-10-28
  • 打赏
  • 举报
回复
尼玛,搞定了,步骤如下: 1.在系统盘 C:\Windows\System32\inetsrv\config 下有一个applicationHost.conf文件 2.在该 ApplicationHost.config 文件中找到在 <requestfiltering></requestfiltering>元素,然后找到该 <hiddensegments></hiddensegments>元素。 3.在该 <hiddensegments></hiddensegments>元素,删除与所请求的 URL 中指定的目录匹配的条目。 目前没法测试,是要将web服务器的配置改掉还是将客户机的改掉。如果是服务器的改掉,那么客户端就不需要改了,如果是客户端要改,就麻烦大了。
该隐叶 2013-10-28
  • 打赏
  • 举报
回复
引用 3 楼 yuwenge 的回复:
直接用浏览器可以下载么?
用IE浏览器不行,用其他浏览器可以,用迅雷等软件也可以

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧