Associating ListT with WebClient

1

How to associate a class List with a WebClient , and when it finishes it calls another WebClient in a index

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 stops saying an internal application error.

    
asked by anonymous 17.01.2015 / 23:17

1 answer

1

What you want (or seems to be) is not such a trivial thing to do right, and passes away from the code posted. I'll put a code that makes the basics practically mounted on top of the documentation examples but this code is not in a position 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;
    }
}

See running on dotNetFiddle .

    
17.01.2015 / 23:52