下载多个文件的WebClient

编程入门 行业动态 更新时间:2024-10-25 18:36:41
本文介绍了下载多个文件的WebClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想下载多个文件,但它不工作,因为我希望的。 谁能告诉我有什么不对这个剧本,因为我已经试过了很多东西,真的不知道该怎么办了。

公共静态无效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()); } }

解决方案

我将使用 System.Net.HttpWebRequest 而不是

这是代码的样子:

私人列表<串GT;网址=新的List<串GT;(); 私人无效btnGo_Click(对象发件人,EventArgs五) { urls.Add(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> <设置> < HttpWebRequest的useUnsafeHeaderParsing =真/> < /设置> < /system>

作为与名碰撞你的问题,你在你的评论说,这应该是改变解决您的变数名称= 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("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> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> </settings> </system>

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

本文发布于:2023-11-12 01:30:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件   WebClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!