0

想知道它是否可能和/或支持将整个对象放置/发布到 REST Web 服务,而不是仅仅一些名称/值对?

如果是这样,也可以放置/发布对象列表吗?

我认为这可能是可能的,因为 GET 请求能够返回对象列表,我想对更新的对象进行“反向”操作(而不是一次发送一个,或者更糟的是,单独发送通过名称/值对片段)?

我知道这是一个非常基本的问题,但到目前为止我采取的方法是尝试对 PUT 进行编码并使其工作(如果 PUT 函数没有参数,则该工作有效,例如:

public class AObjectController : ApiController
{
    public List<int[]> Put()
    {
        List<int[]> ret = new List<int[]>();

        ret.Add(new int[] {-1, 1111});
        ret.Add(new int[] {-2, 2222});

        return ret;
    }

如果我指定单个对象或对象列表,则会出现异常:

    public List<int[]> Put(AObject object)         **CASE 1**
    public List<int[]> Put(List<AObject> objects)  **CASE 2**
    {
        List<int[]> ret = new List<int[]>();

        ret.Add(new int[] { -1, 1111 });
        ret.Add(new int[] { -2, 2222 });

        return ret;
    }

案例 1:公共列表(int[])Put(AObject 对象)


在此处输入图像描述


案例 2:公共列表(int[])放(列表(AObject)对象)


在此处输入图像描述


下面是进行调用的客户端的代码:

    public int writeAll(List<T> data)
    {
        _sendBuffer = 
            JsonConvert.SerializeObject(
                tabletData,
                new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
            );

        byte[] b = StringHelper.GetBytes(_sendBuffer);

        string url = ContructUrlRequest(null, null);

        WebRequest request = WebRequest.Create(url);

        request.Method = "PUT";
        request.ContentType = "application/json";
        request.ContentLength = b.Length;
        request.Credentials = CredentialCache.DefaultCredentials;
        ((HttpWebRequest)request).UserAgent = "...";
        //((HttpWebRequest)request).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(b, 0, b.Length);
            requestStream.Flush();
            requestStream.Close();
        }

        WebResponse response = request.GetResponse();

        if (response == null)
        {

            return -1;
        }

        StreamReader sr = new StreamReader(response.GetResponseStream()); ;

        _recieveBuffer = sr.ReadToEnd();

        List<int[]> _resultData = JsonConvert.DeserializeObject<List<int[]>>(
            _recieveBuffer,
            new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
        );

        return data.Count;
    }

谢谢你。

4

1 回答 1

0

在我的客户端使用来自服务器的序列化编写代码并且它有效

    public int writeAll(List<AObject> aObjects)
    {
        string url = ContructUrlRequest(null, null);

        WebRequest request = WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Put;
        request.ContentType = "application/json; charset=utf-8";            
        request.Credentials = CredentialCache.DefaultCredentials;
        ((HttpWebRequest)request).UserAgent = "going insane";

        JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
        serializerSettings.TypeNameHandling = TypeNameHandling.Objects | TypeNameHandling.Arrays;
        serializerSettings.Converters.Add(new IsoDateTimeConverter());

        JsonSerializer serializer = JsonSerializer.Create(serializerSettings);

        using (Stream requestStream = request.GetRequestStream())
        {
            using (StreamWriter streamWriter = new StreamWriter(requestStream, new UTF8Encoding(false, true)))
            {
                using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter))
                {
                    serializer.Serialize(jsonTextWriter, aObjects);
                }
            }
        }

        WebResponse response = request.GetResponse();

        if (response == null)
        {
            Log.Info(FIDB.TAG_WSBUFFER, "WSBuffer.writeAll: response = NULL");

            return -1;
        }

        StreamReader sr = new StreamReader(response.GetResponseStream());

        _recieveBuffer = sr.ReadToEnd();

        _resultData = JsonConvert.DeserializeObject<List<int[]>>(
            _recieveBuffer,
            new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
        );

        return tabletData.Count;
    }
于 2013-03-08T17:39:09.683 回答