c# - 如何在 visual studio webtest 中将图像作为字节数组发布

标签 c# asp.net-web-api visual-studio-2013 performance-testing

我正在尝试测试我的网络 API,但我无法将图像作为字节数组发布。我该怎么做?我正在使用文件上传参数并指定内容类型 application/octet-stream 但我得到 415 不受支持的媒体类型。如何将图像作为字节数组发布?

这是我的要求:

enter image description here

和表单发布参数属性:

enter image description here

这是我的 web api 发布方法:

enter image description here

这是我的网络测试请求日志:

enter image description here

编辑: @nick_w 有很好的答案,但我找到了另一种方法。我从我的 webtest 文件生成代码。正确的测试请求应该如下所示:

WebTestRequest request2 = new WebTestRequest("http://url/api/SendStream");
request2.Method = "POST";
request2.Headers.Add(new WebTestRequestHeader("Content-Type", "application/octet-stream"));
request2.QueryStringParameters.Add("JobId", this.Context["JobId"].ToString(), false, false);

FileStream oFileStream = new FileStream("4_e.jpg", FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[oFileStream.Length];
oFileStream.Read(bytes, 0, System.Convert.ToInt32(oFileStream.Length));
oFileStream.Close();

BinaryHttpBody request2Body = new BinaryHttpBody();
request2Body.ContentType = "application/octet-stream";
request2Body.Data = bytes;
request2.Body = request2Body;
request2.SendChunked = true;
request2.Timeout = 10000000;

yield return request2;
request2 = null;

最佳答案

我预计您在将流参数绑定(bind)到字节数组时遇到了问题。根据查到的资料herethis问题,请尝试将您的方法更改为:

public async Task<ResponseEntity<SendStreamResponse>> Post([FromUri]int jobId)
{
    byte[] stream = await Request.Content.ReadAsByteArrayAsync();

    return await SendStreamAsync(jobId, stream);
}

关于c# - 如何在 visual studio webtest 中将图像作为字节数组发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171577/

相关文章:

c# - 使用事件目录对用户进行身份验证并获取要在其他地方使用的 token

c# - 如何固定 GridView 中每列的宽度?

c# - 使用错误的 web.config 在 azure 中部署的应用程序

visual-studio-2013 - Visual Studio 2013 Update 5 安装在应用 KB2829760 时卡住

azure - 使用 Azure AD 创建 Azure SQL 数据库登录名/用户

c# - 如何在 Xamarin.Forms 的网格中启用边框

c# - 从颜色创建画笔的正确方法是什么?

asp.net-mvc-4 - ASP.Net MVC 4 WebAPI POST 返回 404

c# - Web API 路由 - 找到多个与请求匹配的操作

c++ - 如何在 Visual C++ 2013 中执行嵌套的 initializer_lists