Using JSON.NET to serialize object into HttpClient's response stream

前端 未结 2 684
温柔的废话
温柔的废话 2020-12-20 00:48

Abstract

Hi, I\'m working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In orde

2条回答
  •  梦毁少年i
    2020-12-20 01:24

    If you define a subclass of HttpContent :

    public class JsonContent:HttpContent
    {
        public object SerializationTarget{get;private set;}
        public JsonContent(object serializationTarget)
        {
            SerializationTarget=serializationTarget;
            this.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        }
        protected override async Task SerializeToStreamAsync(Stream stream, 
                                                    TransportContext context)
        {
            using(StreamWriter writer = new StreamWriter(stream))
            using(JsonTextWriter jsonWriter = new JsonTextWriter(writer))
            {
                JsonSerializer ser = new JsonSerializer();
                ser.Serialize(jsonWriter, SerializationTarget );
            }
    
        }   
    
        protected override bool TryComputeLength(out long length)
        {
            //we don't know. can't be computed up-front
            length = -1;
            return false;
        }
    }
    

    then you can:

    var someObj = new {a = 1, b = 2};
    var client = new HttpClient();
    var content = new JsonContent(someObj);
    var responseMsg = await client.PostAsync("http://someurl",content);
    

    and the serializer will write directly to the request stream.

提交回复
热议问题