title: How to assemble pdf from HTTP stream?
body: <p>I'm using a 3rd party html to pdf conversion (DocRaptor).  You POST your HTML to their site and they respond with the PDF.  The starter code they give you works fine, but it puts the file to your harddrive.  I modified their code to get it to come through the browser and as a file download.  So I'm 100% confident the data I'm getting from the HTTP response is good data.  I can't seem to assemble it back to a useable file.  </p>
  
  <p>I'm reasonably confident the issue is how I'm handing the responseStream data.  It all seems to go wrong once I enter the Try/Catch.  I'm very new to c# and web programming, so I would very much appreciate some guidance from the SO users here.  Thanks.  Here is my code.</p>
  
  <pre><code>            string postData = String.Format(PostFormat,
              (string.IsNullOrEmpty(DocumentContent) ? "document_url" : "document_content"),
              HttpUtility.UrlEncode(string.IsNullOrEmpty(DocumentContent) ? DocumentURL : DocumentContent),
              HttpUtility.UrlEncode(Name),
              HttpUtility.UrlEncode(type),
              HttpUtility.UrlEncode(Test.ToString().ToLower()),
              HttpUtility.UrlEncode(Strict),
              HttpUtility.UrlEncode(PrinceOptions));
  
          var byteArray = Encoding.UTF8.GetBytes(postData);
  
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DocRaptorUrl);
          request.Method = "POST";
          request.ContentType = "application/x-www-form-urlencoded";
          request.ContentLength = byteArray.Length;
          using (var dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); }
  
          System.IO.Stream stream = null;
  
          try 
          {
              using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
              {
                  using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
                  {
  
                      var filepath = @"C:\Users\David\Downloads\UberwriterUSRReport.pdf";
                      HttpContext.Current.Response.ContentType = "application/pdf";
  
                      // let the browser know how to open the PDF document, attachment or inline, and the file name
                      HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));
  
                      stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create);
  
                      CopyStream(responseStream, stream);
  
                      long bytestToRead = stream.Length;
  
                      while (bytestToRead &gt; 0)
                      {
                          if (HttpContext.Current.Response.IsClientConnected)
                          {
                              byte[] buffer = new Byte[10000];  
                              int length = stream.Read(buffer, 0, 10000);
                              HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                              HttpContext.Current.Response.Flush();
  
                              bytestToRead = bytestToRead - length;
                          }
                          else
                          {
                              bytestToRead = -1;
                          }
                      }
                  }
              }
          }
  </code></pre>