将图像从Web Api服务传递到另一个Web Api服务

时间:2013-03-12 14:40:53

标签: asp.net asp.net-web-api

出于安全考虑,我正在构建两个Web Api服务。第一个Web Api应用程序将可以访问图像生成服务,并将充当安全代理。第二个Web Api应用程序将从互联网上调用第一个应用程序并检索图像。

然而,我似乎无法正确地协商图像的通过。我的想法是让安全代理Web API获取图像,然后将其作为字节数组传递给我的其他服务,这将允许用户下载图像。但是,当我的浏览器尝试打开图像时,它总是被破坏。

以下是获取图像的安全代理,我知道这是成功的:

public byte[] Get(string invoice, string Customer)
    {
        object image;
        try
        {
            image = _repo.GetImage(invoice, Customer);
        }
        catch (ApplicationException exc)
        {
            var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(string.Format("No Image with Invoice Number = {0}", invoice.ToString())),
                ReasonPhrase = "Image Not Found"
            };
            throw new HttpResponseException(resp);
        }
        catch (Exception exc)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));

        }
        return (byte[])image;
    }

这将返回一个长度为40133的数组。

调用Web API服务如下所示:

public HttpResponseMessage Get(string invoice, string Customer)
    {           

        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));

        byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;

        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new ByteArrayContent(img);
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
        var disposition = new ContentDispositionHeaderValue("attachment");
        disposition.FileName = "ImageDocument.tif";
        response.Content.Headers.ContentDisposition = disposition;

        return response;

    }

但是,img字节数组的长度为53514。

当浏览器尝试打开图像时,它会告诉我它已损坏。如果我在记事本中打开TIFF,我会得到:

"SUkqAAgAAAASAP4ABAABAAAAAAAAAAABBAABAAAAsAYAAAEBBAABAAAAvgQAAAIBAwABAAAAAQAAAAMBAwABAAAABAAAAAYBAwABAAAAA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QrfNIiMREWdl4zVWRERkQzVECBpNcRyMCz6PhQgZwQGQLjpCIWwgxERGLLYAx//zLWLx4IeDnBnxSGFMRgIeZ4zcaR+KuPM4KeZ6MBTqKcj8YjAQ4IejDoQ4eE07WGnra3p9w07Xhw1s7NHu+0/v+/SQf6/9+cjwp0Z0Z8KeCm4p4IGQwhoz4cwCFBZN8u8s5duXeXTLva7pN6J56l45sf8u8u

SNIP *

任何人都知道我做错了什么?

谢谢!

克里斯

解决

如果有人对利用所识别的解决方案的调用代码感兴趣,那么它是:

public HttpResponseMessage Get(string invoice, string Customer)
    {

        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/tiff"));

        byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;

        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new ByteArrayContent(img);
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
        var disposition = new ContentDispositionHeaderValue("attachment");
        disposition.FileName = "ImageDocument.tif";
        response.Content.Headers.ContentDisposition = disposition;

        return response;


    }

1 个答案:

答案 0 :(得分:3)

使用上面的当前返回类型(byte []),web api的格式化程序可能正在处理它们,因此您会看到意外的响应。

你可以尝试将图像作为ByteArrayContent发送吗?(你需要在这里使用HttpResponseMessage作为返回类型)

示例:

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(..your byte array here...);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");

return response;