title: Copy a PDF Stream to File
body: <p>I'm calling a routine in PHP (TCPDF) from C# via WebRequest using StreamReader. The PDF file is returned as a stream and stored in a string (obv). I know the data being returned to the string is actually a PDF file, as I've tested it in PHP. I'm having a hard time writing the string to a file and actually getting a valid PDF in C#. I know it has something to do with the way I'm trying to encode the file, but the several things I've tried have resulted in 'Not today, Padre' (i.e. they didn't work)</p>
  
  <p>Here's the class I'm using to perform the request (thanks to user 'Paramiliar' for the example I'm using/borrowed/stole):</p>
  
  <pre><code>    public class httpPostData
      {
          WebRequest request;
          WebResponse response;
  
          public string senddata(string url, string postdata)
          {
  
              // create the request to the url passed in the paramaters
              request = (WebRequest)WebRequest.Create(url);
  
  
              // set the method to POST
              request.Method = "POST";
  
              // set the content type and the content length
              request.ContentType = "application/x-www-form-urlencoded";
              request.ContentLength = postdata.Length;
  
              // convert the post data into a byte array
              byte[] byteData = Encoding.UTF8.GetBytes(postdata);
  
              // get the request stream and write the data to it
              Stream dataStream = request.GetRequestStream();
              dataStream.Write(byteData, 0, byteData.Length);
              dataStream.Close();
  
              // get the response
              response = request.GetResponse();
              dataStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(dataStream);
  
              // read the response
              string serverresponse = reader.ReadToEnd();
  
              //Console.WriteLine(serverresponse);
              reader.Close();
              dataStream.Close();
              response.Close();
  
              return serverresponse;
          }
      } // end class httpPostData
  </code></pre>
  
  <p>...and my call to it</p>
  
  <pre><code>httpPostData myPost = new httpPostData();    
  // postData defined (not shown)
  string response = myPost.senddata("http://www.example.com/pdf.php", postData);
  </code></pre>
  
  <p>In case it isn't clear, I'm stuck writing <code>string response</code> to a valid .pdf file. I've tried this (Thanks to user Adrian):</p>
  
  <pre><code>static public void SaveStreamToFile(string fileFullPath, Stream stream)
      {
          if (stream.Length == 0) return;
  
          // Create a FileStream object to write a stream to a file
          using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
          {
              // Fill the bytes[] array with the stream data
              byte[] bytesInStream = new byte[stream.Length];
              stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
  
              // Use FileStream object to write to the specified file
              fileStream.Write(bytesInStream, 0, bytesInStream.Length);
          }
      }
  
  ..and the call to it:
  
      string location = "C:\\myLocation\\";
  
      SaveStreamToFile(location, response);  // &lt;&lt;-- this throws an error b/c 'response' is a string, not a stream. New to C# and having some basic issues with things like this
  </code></pre>
  
  <p>I think I'm close...a nudge in the right direction would be greatly appreciated.</p>