2013-05-08 82 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

相关问题