如何从http请求/响应流中获取PDF

时间:2012-08-14 15:41:49

标签: c# http pdf request response

我有这样的网址" https://site.com/cgi-bin/somescript.pl?file = 12345.pdf& type = application / pdf"。当我转到这个网址时,它会在我的浏览器中以iframe显示pdf。是否可以保存此pdf?我正在考虑获取http响应流并将其转储到文件中。请指教。感谢。

1 个答案:

答案 0 :(得分: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();
            }