1

How do I save a PDF file coming thru a HTTPWebResponse? The file I create seems to be corrupted with different filesize and no content. The code I am using now is as follows :

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());
                StreamWriter streamWriter = new StreamWriter(document_ID+".pdf",false,Encoding.Default);
                streamWriter.Write(streamReader.ReadToEnd());
                streamWriter.Close();
paradox
  • 1,248
  • 5
  • 20
  • 32
  • 2
    Duplicate question: http://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file – Chris Gessler May 06 '12 at 14:32
  • The "Default" encoding may be problematic. Also you may want to use the *using* keyword, for automatic disposal. Also ReadToEnd is a bad idea, because it requires loading the whole stream into memory before writing. Look at the above post, and use that mechanism. – daniel.gindi May 06 '12 at 14:43

0 Answers0