问题描述
我想下载多个文件,但它不工作,因为我希望的。
谁能告诉我有什么不对这个剧本,因为我已经试过了很多东西,真的不知道该怎么办了。
公共静态无效DownloadFile(字符串URL)
{
WebClient的客户端=新的WebClient();
变种名称= url.Substring(url.LastIndexOf('/'))删除(0,1);
的foreach(URL中VAR项)
{
client.DownloadFile(项目,C:\\+姓名);
}
}
私人无效btnGo_Click(对象发件人,EventArgs五)
{
urls.Add(URL1);
urls.Add(URL2);
urls.Add(URL3);
Parallel.ForEach(网址,
新ParallelOptions {MaxDegreeOfParallelism = 10}
DownloadFile);
}
使用(VAR SR =新的StreamReader(HttpWebRequest.Create(URL).GetResponse()。GetResponseStream()))
{
使用(VAR SW =新的StreamWriter(url.Substring(URL .LastIndexOf('/'))))
{
sw.Write(sr.ReadToEnd());
}
}
我将使用而不是
这是代码的样子:
私人列表<串GT;网址=新的List<串GT;();
私人无效btnGo_Click(对象发件人,EventArgs五)
{
urls.Add(http://199.91.152.106/ua0p3fbc5nlg/gg2w2fq4ljc1nnd/MicroCraft_Beta.zip);
Parallel.ForEach(网址,新ParallelOptions {MaxDegreeOfParallelism = 10},DownloadFile);
}
公共静态无效DownloadFile(字符串URL)
{
VAR REQ =(HttpWebRequest的)WebRequest.Create(URL);
变种名称= url.Substring(url.LastIndexOf('/')+ 1);
使用(VAR RES =(HttpWebResponse)req.GetResponse())使用
(VAR FS =新的FileStream使用
(VAR resStream = res.GetResponseStream())(C:\ \+姓名,FileMode.Create,FileAccess.Write,FileShare.None))
{
//保存到文件
变种缓冲=新的字节[8 * 1024]; // 8 KB缓冲区
INT LEN; //读取计数
而((LEN = resStream.Read(缓冲液,0,buffer.Length))大于0)
fs.Write(缓冲液,0,buffer.Length);
}
}
由于的URL你告诉我在评论ISN T使用HTTP协议的正确执行。你必须这样,才能添加到您的配置文件,它的工作(无论是App.config中或Web.config文件,这取决于它是否是一个ASP.Net网站或离线应用):
< system.net>
<设置>
< HttpWebRequest的useUnsafeHeaderParsing =真/>
< /设置>
< /system.net>
作为与名碰撞你的问题,你在你的评论说,这应该是改变解决您的变数名称= url.Substring(url.LastIndexOf('/'))删除(0,1);
成别的东西。
如果你想有一个增量的文件名,你可以使用这样的:
//内部类:
私有静态诠释柜台= 0;
//在你的方法:
变量名称=文件+ System.Threading.Interlocked.Increment(参考计数器)+。html的;
I'm trying to download multiple files but it's not working as I hoped.Can someone tell me what's wrong with this script, because I've tried a lot of things and really don't know what to do anymore.
public static void DownloadFile(string url)
{
WebClient client = new WebClient();
var name = url.Substring(url.LastIndexOf('/')).Remove(0, 1);
foreach (var item in urls)
{
client.DownloadFile(item, "C:\\" + name);
}
}
private void btnGo_Click(object sender, EventArgs e)
{
urls.Add("url1");
urls.Add("url2");
urls.Add("url3");
Parallel.ForEach(urls,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
DownloadFile);
}
using (var sr = new StreamReader(HttpWebRequest.Create(url).GetResponse().GetResponseStream()))
{
using (var sw = new StreamWriter(url.Substring(url.LastIndexOf('/'))))
{
sw.Write(sr.ReadToEnd());
}
}
I would use a System.Net.HttpWebRequest
instead.
This is what the code would look like:
private List<string> urls = new List<string>();
private void btnGo_Click(object sender, EventArgs e)
{
urls.Add("http://199.91.152.106/ua0p3fbc5nlg/gg2w2fq4ljc1nnd/MicroCraft_Beta.zip");
Parallel.ForEach(urls, new ParallelOptions { MaxDegreeOfParallelism = 10 }, DownloadFile);
}
public static void DownloadFile(string url)
{
var req = (HttpWebRequest)WebRequest.Create(url);
var name = url.Substring(url.LastIndexOf('/') + 1);
using (var res = (HttpWebResponse)req.GetResponse())
using (var resStream = res.GetResponseStream())
using (var fs = new FileStream("C:\\" + name, FileMode.Create, FileAccess.Write, FileShare.None))
{
// Save to file
var buffer = new byte[8 * 1024]; // 8 KB buffer
int len; // Read count
while ((len = resStream.Read(buffer, 0, buffer.Length)) > 0)
fs.Write(buffer, 0, buffer.Length);
}
}
Because the URL you told me in the comment isn't using a proper implementation of the HTTP protocol. You'll have to add this to your config file in order for it to work (either App.config or Web.config, depending on if it's an ASP.Net site or offline application):
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
As to your problem with names colliding which you said in your comment, this should be resolved by changing your var name = url.Substring(url.LastIndexOf('/')).Remove(0, 1);
into something else.
If you want to have an incremental filename, you could use this:
// Inside your class:
private static int counter = 0;
// In your method:
var name = "file" + System.Threading.Interlocked.Increment(ref counter) + ".html";
这篇关于下载多个文件的WebClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!