当客户端收到时,从服务器返回的字节数组是不同的

时间:2015-11-08 13:48:34

标签: c# rest serialization bytearray asp.net-web-api

我有一个REST API试图从我的web api调用(获取)一个字节数组。 这是我的客户代码:

left join

这是我的服务器API:

Uri uri = new Uri(URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Binary"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result

服务器返回的字节数组是:

enter image description here

客户端收到的字节数组是:

enter image description here

正如您所看到的那样,它的大小和价值都是错误的。

我假设这是媒体标题,目前设置为:

[HttpGet]
public byte[] Get(Guid companyRef)
{
    Mapper.CreateMap<Services.Group,Models.Group>();
    var test = Mapper.Map<List<Models.Group>>(groupRepository.Get(companyRef));
    var t = Compression.Serialize(test);
    return t;
}


public static byte[] Serialize(this Object obj)
{
    if (obj == null)
    {
        return null;
    }

    using (var memoryStream = new MemoryStream())
    {
        var binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(memoryStream, obj);
        var compressed = Compression.Zip(memoryStream.ToArray());

        return compressed;
    }
}

使用Fiddler似乎认为它正在期待Json。所以我把它改成了     '应用程序/ JSON'

那不行。

我也使用了base64Binary,但也没用。

我还能尝试什么?

附加: 我直接在浏览器中输入api的结果:

httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Binary"));

更多信息:

如果改变这个:

<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk+fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z+YzdvrzuZEq7D8OI4BBXhCugH+ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF+zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/hQ4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA==
</base64Binary>

对此:

 var result = response.Content.ReadAsByteArrayAsync().Result;
    byte[] resultFromServer = Encoding.ASCII.GetBytes("H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk+fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z+YzdvrzuZEq7D8OI4BBXhCugH+ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF+zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/hQ4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA==");
    byte[] actualBytes = Convert.FromBase64String(Encoding.ASCII.GetString(resultFromServer));

我收到错误:

输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符。

这是结果的值

“H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g / 4CDlhb64K9Dq304jjWXTK / rbXePb CKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQ xPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1 + tXAVn6b7VGvIKk7j0EAyUKk + fKle93ZlM / hRKn9kCNAaUtl4Ok / 9tYDrtYQdCKmzA9aVTHyCU788z + YzdvrzuZEq7D8OI4BBXhCugH + ZtPP725SiBkU / SJxEkAck6DeWJBiQxSp / iOvKsK6 + / XsKJ / zw7g / gAXF6qJR1gz / Pizake1qq gov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF + zCwiDgG2N / mbiKEh / ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF // g11F6e1qLGy1MNNj3LmwLZfD aZM6puYFC3GzRQUN / 66K3KeLFEoy41eLChvvaxd2hWY / BFMrMxd0WGjXoly70HqF7M5wmdnu / h的 Q4a9HMX + oAiDqwBkbS4uOE + dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA ==“

来自Debug.Write:

“H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g / 4CDlhb64K9Dq304jjWXTK / rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1 + tXAVn6b7VGvIKk7j0EAyUKk + fKle93ZlM / hRKn9kCNAaUtl4Ok / 9tYDrtYQdCKmzA9aVTHyCU788z + YzdvrzuZEq7D8OI4BBXhCugH + ZtPP725SiBkU / SJxEkAck6DeWJBiQxSp / iOvKsK6 + / XsKJ / zw7g / gAXF6qJR1gz / Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF + zCwiDgG2N / mbiKEh / ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF // g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN / 66K3KeLFEoy41eLChvvaxd2hWY / BFMrMxd0WGjXoly70HqF7M5wmdnu / hQ4a9HMX + oAiDqwBkbS4uOE + dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA ==“

为了使这最终成功,我必须这样做:

var result = response.Content.ReadAsStringAsync().Result;
byte[] resultFromServer = Encoding.ASCII.GetBytes(result);
byte[] actualBytes = Convert.FromBase64String(Encoding.ASCII.GetString(resultFromServer));

由于

2 个答案:

答案 0 :(得分:2)

由于您以字符串形式获取响应,因此只需解码该Base64字符串。

var result = response.Content.ReadAsStringAsync()。Result; byte [] actualBytes = Convert.FromBase64String(result);

答案 1 :(得分:2)

经过一番研究,我发现asp网络核心团队不想解决此问题(https://github.com/aspnet/Mvc/issues/7926),要解决您的问题,您必须返回“ FileStreamResult”。

以此更改您的控制器

[HttpGet]
public FileStreamResult Get(Guid companyRef)
{
    Mapper.CreateMap<Services.Group, Models.Group>();
    var test = Mapper.Map<List<Models.Group>>(groupRepository.Get(companyRef));
    var t = Compression.Serialize(test);
    return File(new MemoryStream(t), "application/octet-stream");
}