Inquiridor
How to do download file by HttpWebRequest - It's working well so!

Pergunta
-
Following example as to do download using HttpWebRequest instead Webclient
//I get this information from appSetings() -- <add key="input" value="C:\Desenvolvimento\input\D_quina.zip"/> string input = System.Configuration.ConfigurationManager.AppSettings["input"].ToString(); byte[] result = null; byte[] buffer = new byte[4097]; Uri uri = new Uri("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_quina.zip"); HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(uri); try { webReq.CookieContainer = new CookieContainer(); webReq.Method = "GET"; MemoryStream oMemory = new MemoryStream(); WebResponse response = webReq.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); int count = 0; do { count = stream.Read(buffer, 0, buffer.Length); oMemory.Write(buffer,0,count); if (count == 0) { break; } } while (true); result = oMemory.ToArray(); FileStream ofile = new FileStream(input, FileMode.OpenOrCreate, FileAccess.ReadWrite); ofile.Write(result,0,result.Length); ofile.Close(); oMemory.Close(); stream.Close(); extractFile(); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5)); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3)); lerHtmlQuinaResult(); } catch (Exception ex) { Console.Write(ex.Message); }
On this form I'm able to do download by url with zip file on this example.
I hope that this information can be useful.
If you want extract the zip file you can to do so:
public static void extractFile() { // input - path where is your zip file // outpu - path where will be done the file extraction ZipFile.ExtractToDirectory(input,output); }
Thank you,
Edmilson
- Editado Edmls30 terça-feira, 29 de dezembro de 2015 02:23
Todas as Respostas
-
Para efetuar um download de uma maneira simples usando c#:
using System.Net; WebClient webClient = new WebClient(); webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");
Ou usando metodos assincronos:
private void btnDownload_Click(object sender, EventArgs e) { WebClient webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt"); } private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBar.Value = e.ProgressPercentage; } private void Completed(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("Download completed!"); }
- Sugerido como Resposta Jean LLopes terça-feira, 5 de janeiro de 2016 00:29
- Não Sugerido como Resposta Jean LLopes terça-feira, 5 de janeiro de 2016 00:29
- Sugerido como Resposta Jean LLopes terça-feira, 5 de janeiro de 2016 00:30
-
Olá Jean,
Obrigado pela sua resposta.
porém desta forma para download usando webclient, eu tive problemas com várias tentativas de requisições, e mesmo usando as propriedades de quantidades máximas de solicitações configurada eu obtive o mesmo erro.
E da forma que publiquei eu obtive sucesso..
Abraço.
Edmilson Lopes .NET Development Specialist
-
Olá Jean,
Obrigado pela sua resposta.
porém desta forma para download usando webclient, eu tive problemas com várias tentativas de requisições, e mesmo usando as propriedades de quantidades máximas de solicitações configurada eu obtive o mesmo erro.
E da forma que publiquei eu obtive sucesso..
Abraço.
Edmilson Lopes .NET Development Specialist
Fulvio C