Downloading pdf file using WebRequests

后端 未结 4 1357
夕颜
夕颜 2020-12-15 09:20

I\'m trying to download a number of pdf files automagically given a list of urls.

Here\'s the code I have:

HttpWebRequest request = (HttpWebRequest)W         


        
相关标签:
4条回答
  • 2020-12-15 09:33

    Why not use the WebClient class?

    using (WebClient webClient = new WebClient())
    {
        webClient.DownloadFile("url", "filePath");
    }
    
    0 讨论(0)
  • 2020-12-15 09:46

    Your question asks about WebClient but your code shows you using Raw HTTP Requests & Resposnses.

    Why don't you actually use the System.Net.WebClient ?

    using(System.Net.WebClient wc = new WebClient()) 
    {
        wc.DownloadFile("http://www.site.com/file.pdf",  "C:\\Temp\\File.pdf");
    }
    
    0 讨论(0)
  • 2020-12-15 09:47
            private void Form1_Load(object sender, EventArgs e)
            {
      
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                webClient.DownloadFileAsync(new Uri("https://www.colorado.gov/pacific/sites/default/files/Income1.pdf"), @"output/" + DateTime.Now.Ticks ("")+ ".pdf", FileMode.Create);
            }
    
            private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                progressBar = e.ProgressPercentage;
            }
    
            private void Completed(object sender, AsyncCompletedEventArgs e)
            {
                MessageBox.Show("Download completed!");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 09:50

    Skip the BinaryReader and BinaryWriter and just copy the input stream to the output FileStream. Briefly

    var fileName = "output/" + date.ToString("yyyy-MM-dd") + ".pdf";
    using (var stream = File.Create(fileName))
      resp.GetResponseStream().CopyTo(stream);
    
    0 讨论(0)
提交回复
热议问题