【问题标题】:C# WebAPI redirect to a Link that displays an ImageC# WebAPI 重定向到显示图像的链接
【发布时间】:2014-03-02 10:29:20
【问题描述】:
    public HttpResponseMessage getLogoPath(int ID)
    {
        String urlsubfix = dataManager.getMobileLogoPath(ID);
        String urlToReturn =  PictureController.urlprefix  +urlsubfix;
        var response = Request.CreateResponse(HttpStatusCode.Redirect);
        response.Headers.Location = new Uri(urlToReturn);
        return response;
    }

我希望有一个 API 调用来返回图像,我是如何实现的,即重定向到另一个具有图像的链接,例如:

http://steve.files.wordpress.com/2006/03/Matrix%20tut%202.jpg

我正在尝试使用 PostMan 来测试它,它给了我一个损坏的图像。我尝试了四处寻找,但仍然找不到解决方案。

-----编辑需要这个的部分代码

    public HttpResponseMessage GetCustomerBarCodeLogo(String ID)
    {
        Barcode BC = new Barcode();
        Image img =BC.Encode(TYPE.CODE128, "123456789");
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        ImageConverter imageConverter = new ImageConverter();
        byte[] resourceByteArray = (byte[])imageConverter.ConvertTo(img, typeof(byte[]));
        MemoryStream dataStream = new MemoryStream(resourceByteArray);
        result.Content = new StreamContent(dataStream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        return result; 
    }

这会返回一个损坏的图像。

------最新更新

我发现了问题所在。发生的情况是 SSL 基本身份验证存在问题。发生的情况是,当客户端第一次想要传递请求时,身份验证设置正确。但是,当它尝试获取由内部代码触发的另一个请求的图像时,该请求没有 autherizationHeader 并且未经过身份验证。我不知道第二个事件在哪里触发,所以我不知道如何手动设置该标题。

【问题讨论】:

  • 重定向可能没有发送 auth 标头。您是否尝试过从设置了凭据缓存的常规客户端进行重定向?
  • 我不认为这是问题所在,因为即使我尝试从目录加载图像并以字节为单位返回它仍然不显示
  • 但是当你直接访问 URL 时它确实有效,对吗?只有当您重定向时,URI 才会损坏?
  • 我发现了问题所在。发生的情况是 SSL 基本身份验证存在问题。发生的情况是,当客户端第一次想要传递请求时,身份验证设置正确。但是,当它尝试获取由内部代码触发的另一个请求的图像时,该请求没有 autherizationHeader 并且未经过身份验证。我不知道第二个事件在哪里触发,所以我不知道如何手动设置该标题。
  • 默认情况下,HttpClient 内部的 HttpClientHandler 将执行自动重定向。如果要自动发送凭据,则需要在 CredentialCache 中为要访问的所有主机设置所有凭据。 HttpClientHandler 将在凭据缓存中查找您正在调用的主机,如果找到凭据,则会将它们应用于重定向请求。

标签: c# image asp.net-web-api http-headers http-redirect


【解决方案1】:

您无法重定向以响应此网址中的图像,您的呼叫者将能够更改位置。

第一个解决方案,尝试直接使用web api的图像结果,作为示例:

public HttpResponseMessage getLogoPath(int id)
{
    string pathImage = // a way to get the imagem path..

    // create response
    HttpResponseMessage response = new HttpResponseMessage(); 

    // set the content (image)
    response.Content = new StreamContent(new FileStream(pathImage));

    // set the content type for the respective image
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); // or jpg, gif..

    return response;
}

或者使用移动的状态码,对于 smaple:

public HttpResponseMessage getLogoPath(int ID)
{
    String urlsubfix = dataManager.getMobileLogoPath(ID);
    String urlToReturn =  PictureController.urlprefix + urlsubfix;

    var response = Request.CreateResponse(HttpStatusCode.Moved);
    response.Headers.Location = new Uri(urlToReturn);
    return response;
}

【讨论】:

【解决方案2】:

这是另一种可能对您有用的方法。这是我使用的技术的接近。

public HttpResponseMessage GetCustomerBarCodeLogo(String ID)
{
    Barcode BC = new Barcode();
    Image img =BC.Encode(TYPE.CODE128, "123456789");

    var dataStream = new MemoryStream();
    img.Save(dataStream, ImageFormat.Png);
    dataStream.Position = 0;

    var result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(dataStream);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
    return result; 
}

【讨论】:

    【解决方案3】:

    您可以使用 FileResult 例如

    public FileResult GetLogo(string imageName)
    {
        return File(Server.MapPath("~/App_Data/Images/") + imageName, "image");
    }
    

    在这种情况下,File 函数采用文件路径和内容类型并返回文件响应。

    【讨论】:

    • 这不是我想要完成的,我想重定向到另一个有图片的 Http url
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    相关资源
    最近更新 更多