Associating List<T> with Webclient

Asked

Viewed 110 times

1

How to associate a class List to a Webclient, and at its end calls another Webclient in a index different

List< WebClient > clients;

void BaixarTudo(List< string > urls){
  for(int i=0;i < urls.Count; i++){
     clients=new List< WebClient >(urls.Count);
  }
}

But the system interrupts saying an internal error of the application.

  • I could not understand what you want to do. Explain better the purpose and what is this internal error of the application.

  • I did a test with the code and it was no mistake. If you do what you want I don’t know why I didn’t understand the goal but the code really doesn’t do anything useful. https://dotnetfiddle.net/OYDNCi

  • type I want when finished downloading the first Webclient automatically start the other download!

1 answer

1


What you want (or appear to be) is not something so trivial to do right, and goes far from the code posted. I will put a code that makes the basic practically mounted on top of the examples of the documentation but this code is not in condition to go to production.

using System.Console;
using System.Net;
using System.IO;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        var paginas = BaixarTudo(new List<string>{"http://google.com", "http://yahoo.com"});
        foreach(var pagina in paginas) {
            WriteLine(pagina);
        }
    }

    public static List<string> BaixarTudo(List<string> urls) {
        var paginas = new List<string>(urls.Count);
        foreach(var url in urls) {
            var client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            using (var data = client.OpenRead(url)) {
                using (var reader = new StreamReader(data)) {
                    paginas.Add(reader.ReadToEnd());
                }
            }
        }
        return paginas;
    }
}

Behold working in the .NET Fiddle. Also put on the Github for future reference.

  • Ta right but what I want is to download multiple files automatically!

  • That’s exactly what you’re doing. If this is not what you really want, you have to ask your questions with more quality, you have to explain what you want in detail, clearly. What you asked is answered. What you really want only you know and has not been written anywhere, so there is no way anyone can guess what it would be, we can only answer on top of what is actually written.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.