the_transltr
the_transltr

Reputation: 67

How to get PDF from http request/response stream

I have a url like this "https://site.com/cgi-bin/somescript.pl?file=12345.pdf&type=application/pdf". When i go to this url it will dispaly an pdf in my browser in an iframe. Is it possible to save this pdf? I am thinking of getting the http response stream and dump it into a file. Please advice. Thanks.

Upvotes: 3

Views: 8832

Answers (1)

parapura rajkumar
parapura rajkumar

Reputation: 24403

Something like this would work

             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();
            }

Upvotes: 2

Related Questions