2013-05-08 83 views
3

我從REST API在如下因素代碼行獲得PDF文件如何獲得REST API的PDF文件在C#中


response = request.GetResponse() as HttpWebResponse;// HttpWebResponse response 

我用流讀取器獲得響應

  if (response != null) 
      { 
       rchResponseHeader.Text = response.Headers.ToString(); 
       //string resBody = null; 
       using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       { 
        resBody = reader.ReadToEnd();//string resBody 
       } 
      } 

我使用filedialogBox和pdf擴展名如下保存文件

 if (saveDialogBox.ShowDialog() == DialogResult.OK) 
      { 

       string name = saveDialogBox.FileName; 
       File.WriteAllText(name, resBody); 
      } 

文件保存成功,但無法打開文件:我相信我用於讀取文件和寫入的方式並不合適。有沒有辦法做到這一點 謝謝

回答

4

您正在讀取和寫入文本的二進制文件。這是行不通的。

使用這樣的事情,而不是:

if (response != null) 
{ 
    rchResponseHeader.Text = response.Headers.ToString(); 

    if (saveDialogBox.ShowDialog() == DialogResult.OK) 
    { 
     using(var fileStream = File.Open(name, ...)) 
     { 
      response.GetResponseStream().CopyTo(fileStream); 
     } 
    } 
} 
+0

感謝丹尼爾它的工作原理 – Bathiya 2013-05-09 09:55:58

相關問題