c# - 如何以正确的方式通过 http 发送二进制数据?
问题描述
我在 C# 中有以下代码,它将字符串从一种编码转换为另一种编码,并通过 http 发送响应。
代码中的一切工作正常。我只是有点困惑
当我通过 http 返回它时,它会转换为文本表示形式。我以为这只会发送原始字节数组?
为什么要转换?我应该永远不要尝试发送原始字节数组,还是在通过 http 通信时有这种情况?或者是文本表示和application/octet-stream
通过http发送二进制数据的方式?
byte[] fromBytes = fromEncoding.GetBytes(body);
byte[] toBytes = Encoding.Convert(fromEncoding, toEncoding, fromBytes);
var response = req.CreateResponse(HttpStatusCode.OK);
MemoryStream ms = new MemoryStream(toBytes);
response.Content = new StreamContent(ms);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
解决方案
推荐阅读
- ios - i如何通过 Secure Enclave 实现安全的 Touch ID 身份验证?
- java - Android:使用 Koin 覆盖子片段中的视图模型对象?
- powershell - 如何使用 Powershell 在 GCP 虚拟机上启用虚拟显示器?
- visual-studio - VS2019+Qt5:UI 未更新
- amazon-web-services - 在 AWS Athena 中使用小于运算符不起作用
- python - KeyError:“['index'] 均不在列中”
- security - google cast 自定义消息的安全性如何?
- r - R:在绘图中自动缩放图表轴时如何忽略形状?
- python - 被继承的类能看到父类上值的变化吗?
- python - 期待“字符串”,在请求响应中得到“未定义”