使用ASP.Net WebAPI 2通过HTTP传输原始字节 [英] Transferring raw bytes over http using the ASP.Net WebAPI 2

查看:57
本文介绍了使用ASP.Net WebAPI 2通过HTTP传输原始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的ASP.Net WebAPI 2方法,希望通过HTTP发送一小部分字节:

I have a very simple ASP.Net WebAPI 2 method that I hoped would send a small array of bytes over HTTP:

[Route("api/assemblybytes")]
public byte[] GetAssemblyBytes()
{
    //byte[] bytes = null;
    //if (File.Exists("BasePCBScreenAssembly.dll"))
    //{
    //    using (var fs = File.Open("BasePCBScreenAssembly.dll", FileMode.Open, FileAccess.Read))
    //    {
    //        fs.Read(bytes, 0, (int)fs.Length);
    //    }
    //}
    //return bytes;
    return new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
}

为了测试该功能,我已经用一个包含字节0x20的简单数组替换了预期的代码(在上面进行了注释)七次.要测试我的代码,我有一个简单的网页:

To test the function I have replaced the intended code (commented out in the above) with a simple array containing the byte 0x20 seven times. To test my code I have a simple web page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Assembly Distribution WebAPI Test Page</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
    <script>
        var uri = 'api/assemblybytes';
        $(document).ready(function () {
            $.getJSON(uri)
                .done(function (data) {
                    var bytes = [];
                    for (var i = 0; i < data.length; ++i) {
                        bytes.push(data.charCodeAt(i));
                    }
                    $('#AssemblyBytes').text(bytes)
                });
        });
    </script>
</head>
<body>
    <div>
        <h2>GetAssemblyBytes</h2>
        <pre id='AssemblyBytes'/>
    </div>
</body>
</html>

我希望使用该网页查看在字节数组的开头或结尾添加了哪些字节或文本.但是相反,我看到了这一点,并将字节返回到了网页:

I was hoping to use the web page to see what bytes or text were added to the beginning or the end of the byte array. But instead I see this and the bytes returned to the web page:

73,67,65,103,73,67,65,103,73,65,61,61

所以我的0x20(十进制为32)甚至都不会显示一次.

So my 0x20 (32 in decimal) does not even show up once.

(注意,这可能是一个人为的例子.实际的客户端不是网页,而是.Net Micro Framework嵌入式设备,它将(希望)从WebAPI下载小的动态程序集作为字节数组.)

(N.B. This may seem a contrived example. The actual client will not be a web page but a .Net Micro Framework embedded device that will (I hope) download small dynamic assemblies as byte arrays from the WebAPI.)

我错了什么或被遗忘了什么?使用ASP.Net WebAPI通过HTTP传输字节数组的最佳方法是什么?

What have I got wrong or omitted? What is the best way to transfer an array of bytes over HTTP using the ASP.Net WebAPI?

推荐答案

如果不返回 HttpResponseMessage ,WebAPI将根据以下内容将值序列化为某种格式(JSON,XML等)接受标题和已安装的媒体类型格式化程序.

If you do not return a HttpResponseMessage, WebAPI will serialize the value into some format (JSON, XML, etc.) depending on Accept headers and installed media type formatters.

要发送原始数据:

HttpResponseMessage res = CreateResponse(HttpStatusCode.OK);
res.Content = new ByteArrayContent(bytes);
return res;

或者,因为您有 Stream :

HttpResponseMessage res = CreateResponse(HttpStatusCode.OK);
res.Content = new StreamContent(File.Open(...));
return res;

这篇关于使用ASP.Net WebAPI 2通过HTTP传输原始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆