本文介绍了如果指定了Accept:application/json,则响应的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的Http方法是GET,我正在调用相同的webservice方法(具有相同的参数).

The Http method used is GET and I'm calling the same webservice method (with same parameters).

但是如果我添加请求以application/json请求Accept标头,则输出会有所不同.原因是我的名为User的对象中的一个Bitmap字段,其中保存有头像图像.

But if I add to request an Accept header with application/json, the output differs. The cause is a Bitmap field in my object named User, which holds an avatar image.

如果我省略了Accept application/json标头,则这是简化的输出(XML):

If i leave out the Accept application/json header, this is the simplified output (XML):

<!-- language: lang-xml -->
<ArrayOfUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XTraN4ForcesFSDomain.Domain">
<User>
    <Id>02ddf1e4-ad76-4778-8887-a186014939f8</Id>
    <Avatar xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Drawing" i:nil="true" />
    <IsActive>false</IsActive>
    <LastAccess>0001-01-01T00:00:00</LastAccess>
    <Username>quisquam</Username>
    <!-- Other properties -->
</User>
<User>
    <Id>17db833c-5008-44f0-a713-a186014c22a5</Id>
    <Avatar xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Drawing">
        <Data xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:base64Binary" xmlns="">iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABGdBTUEAAK/INwWK6[...]BIJS/Wd6Pgu/mOoS/HADwfwFUI4VkJHOgAgAAAABJRU5ErkJggg==</Data>
    </Avatar>
    <IsActive>false</IsActive>
    <LastAccess>0001-01-01T00:00:00</LastAccess>
    <Username>labore</Username>
</User>
</ArrayOfUser>

嗯,这很好!图像(Base64)在那里.如果我更改获取JSON的请求,它将不返回任何图像,仅返回其表示的类的名称:

Well, this is just fine! The image (Base64) is there. If I change my request to get JSON, it returns no image, just the name of the class that it represents:

<!-- language: lang-json -->
[
    {
        "Username": "quisquam",
         "LastAccess": "0001-01-01T00:00:00",
        "IsActive": false,
        "Avatar": null,
        "Id": "02ddf1e4-ad76-4778-8887-a186014939f8"
   },
   {
       "Username": "reiciendis",
       "LastAccess": "0001-01-01T00:00:00",
       "IsActive": false,
       "Avatar": "System.Drawing.Bitmap",
       "Id": "17db833c-5008-44f0-a713-a186014c22a5"
   },
]

Webservice方法是

The webservice mthod is

<!-- language: lang-c# -->
public IQueryable<User> Get()
{
    // return stuff (no big deal here)
}

代码是相同的,那么为什么JSON不返回应该的base64字符串呢?

The code is the same, so why won't JSON return a base64 string like it should?

推荐答案

LukeH 所述,将我的位图转换为字节数组解决了我的问题.

As LukeH said, converting my Bitmap to byte array solved my issues.

这篇关于如果指定了Accept:application/json,则响应的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:12