c# - UnityWebRequest 字节数组
问题描述
我正在尝试通过网络摄像头捕获的每一帧,使用要在 NodeJS API 中处理的发布请求发送图像字节。
使用此代码:
Debug.Log(imageBytes.Length); //Prints a number
Debug.Log(imageBytes); //Prints the array type (?)
Debug.Log(imageBytes[1]); //Prints the byte
UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
UploadHandlerRaw handler = new UploadHandlerRaw(imageBytes);
handler.contentType= "application/x-www-form-urlencoded";
www.uploadHandler = handler;
Debug.Log(www.uploadHandler.data[1]);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
string jsonResponse = www.downloadHandler.text;
Debug.Log(jsonResponse);
但是,当我console.log
在 API 中执行它时,req.body
它会打印
{}
. 显然,数据不是由UnityWebRequest
.
对此有什么想法吗?
解决方案
我找到了一种让这个工作的方法。
需要将Unity中的代码改成:
using (UnityWebRequest www = UnityWebRequest.Post(url, webForm))
{
www.SetRequestHeader("Content-Type", "text/html");
www.uploadHandler = new UploadHandlerRaw(imageBytes);
www.uploadHandler.contentType = "text/html";
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
}
在 Node.JS 中:
app.use(cors());
var options = {
inflate: true,
limit: '3000kb',
type: 'text/html'
};
app.use(bodyParser.raw(options));
推荐阅读
- flutter - Flutter Stateful Widget 传递 Null
- amazon-web-services - AWS 角色和策略 - 限制对一个存储桶的访问
- android - 在android studio中开发程序时遇到问题
- python - 为什么我在 python firebase-admin initialize_app() 上收到此错误?
- go - 上下文级联测试
- reactjs - 通过 Spring Cloud Gateway 访问时 React 应用程序不呈现
- html - 为什么溢出:隐藏;留下空白CSS?
- parameters - 快速查看动态添加或删除尺寸
- python - 如何可视化 rviz 发布点?
- ios - 使用多线程时,Swift viewController 仍然加载很长时间