C# 如何在HTTP正文中使用RestSharp打开作为二进制内容接收的文件';s联合编辑

C# 如何在HTTP正文中使用RestSharp打开作为二进制内容接收的文件';s联合编辑,c#,rest,restsharp,C#,Rest,Restsharp,根据要求,我必须调用REST服务,它将以HTTP正文中文件的二进制内容和内容类型头的适当值的形式发回附件或文件,我需要在其特定的编辑器中打开内容 我正在使用RestSharp,下面是相同的代码:例如,如果附件是.doc- var client = new RestClient(Properties.Settings.Default.RestServicesUrl + Constants.RestTicketServices); var restRq = new RestRequest(Const

根据要求,我必须调用REST服务,它将以HTTP正文中文件的二进制内容和内容类型头的适当值的形式发回附件或文件,我需要在其特定的编辑器中打开内容

我正在使用RestSharp,下面是相同的代码:例如,如果附件是.doc-

var client = new RestClient(Properties.Settings.Default.RestServicesUrl + Constants.RestTicketServices);
var restRq = new RestRequest(Constants.SearchTicket);
restRq.Method = Method.GET;
restRq.RequestFormat = DataFormat.Json;
restRq.AddParameter("account_id", "10");
client.Authenticator = new HttpBasicAuthenticator(Properties.Settings.Default.RestAuthenticationUserName, Properties.Settings.Default.RestAuthenticationPass);
IRestResponse rest = client.Execute(restRq);
因此,在这次通话之后:

rest.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"
rest.Content = has binary data for the word document
等等

基本上,我正在寻找如何读取二进制内容并在关联编辑器中打开它们

.xls-Excel .doc-Word .pdf-Adobe等…

您必须先将文件(即二进制内容)保存到磁盘。那么,只需打开它:

File.WriteAllBytes(filePath, response.Content);
System.Diagnostics.Process.Start(filePath);

已经晚了一年,但最近我搜索了OP问题的答案并找到了它-只需替换代码的最后一行:

byte[] bytes = client.DownloadData(restRq);
如果您还想拥有iRestress,则可以通过以下方式获取RawBytes:

IRestResponse rest = client.Execute(restRq);
byte[] bytes = rest.RawBytes;

更有趣的方法在中解释。

File.writealBytes的签名需要一个字节[],response.Content是string,因此,此代码无法编译。