我写了我的第一个ASP.NET Core Web API服务器。路由正在工作,并且正在发送/接收。我想发送一个原始的Byte数组来响应Get,我想绕过任何序列化(以及我的WPF客户端中的任何反序列化)。举例来说,在我的服务器:如何使用ASP.NET Core Web API发送/接收原始字节数组?
[HttpGet("api/data")]
public Byte[] GetData()
{
Byte[] data = new Byte[]{1, 2, 3, 4, 5};
return data; // No JSON, BSON or anything else...just my array. Is it done this way?
}
在我的WPF客户端,我想用HttpClient的类来接收阵列 - 不需要反序列化:
// Assume an instantiated and initialized HttpClient object called "httpCLient"
HttpResponseMessage response = httpClient.GetAsync("/api/data").Result;
// Now what?
我已经走遍了互联网,但我还没有找到一个这样的例子,我一直无法自己弄清楚。请有人告诉我这是怎么完成的?
更新:我相信客户端的Byte []检索可以通过“HttpContent.ReadAsByteArrayAsync()”来完成,如下所示:Convert HttpContent into byte[]。然而,我仍然不确定如何从ASP.NET Core Web API端发送原始(非序列化的)Byte []。
可能重复的[从HttpResponseMessage获取内容/消息](http://stackoverflow.com/questions/15936180/getting-content-message-from-httpresponsemessage) –
感谢科林。我认为在客户端,我可以通过以下方式检索字节[]: 字节[] data = httpResponseMessage.Content.ReadAsByteArrayAsync()。 但是,我无法测试这个,因为我仍然不确定如何从服务器发送原始字节[]。 – LKeene
http://stackoverflow.com/questions/23884182/how-to-get-byte-array-properly-from-an-web-api-method-in-c检查Luaan的答案。使用ByteArrayContent –