2012-08-14 93 views
2

我有這樣的網址「https://site.com/cgi-bin/somescript.pl?file=12345.pdf & type = application/pdf」。當我去這個網址時,它會在我的瀏覽器中在iframe中顯示pdf。是否可以保存這個pdf?我正在考慮獲取http響應流並將其轉儲到文件中。請指教。謝謝。如何從http請求/響應流獲取PDF

回答

2

像這樣的工作

   const string FILE_PATH = "C:\\foo.pdf"; 
      const string DOWNLOADER_URI = "https://site.com/cgi-bin/somescript.pl?file=12345.pdf&type=application/pdf"; 

      using (var writeStream = File.OpenWrite(FILE_PATH)) 
      { 
       var httpRequest = WebRequest.Create(DOWNLOADER_URI) as HttpWebRequest; 
       var httpResponse = httpRequest.GetResponse(); 
       httpResponse.GetResponseStream().CopyTo(writeStream); 
       writeStream.Close(); 
      }