如何通过http发送和接收不可思议的json数据大小?

时间:2022-08-26 19:06:00

I'm implementing a Spring HTTP server, and a C# client to request/response some sets of datas. I found out that sending expectable size of datas are easy, but what shall I do if the data response from the server is unexpectable?

我正在实现一个Spring HTTP服务器和一个C#客户端来请求/响应一些数据集。我发现发送可预期的数据大小很容易,但如果来自服务器的数据响应是不可理解的,我该怎么办?

For example, the server will respond with three Strings : [data="",x="",y=""]. But, it can send more than one set of that. Like this. [data1="", x1="",y1=""], [data2="", x2="",y2=""], [data3="", x3="",y3=""]. I don't think this isn't a smart approach to solve these kind of issue. Please, can someone introduce me with another one?

例如,服务器将响应三个字符串:[data =“”,x =“”,y =“”]。但是,它可以发送多套。喜欢这个。 [data1 =“”,x1 =“”,y1 =“”],[data2 =“”,x2 =“”,y2 =“”],[data3 =“”,x3 =“”,y3 =“”] 。我不认为这不是解决这类问题的聪明方法。拜托,有人可以介绍另一个吗?

1 个解决方案

#1


1  

For the C# client, you can use WebRequest to retrieve your data, and Json.Net to deserialize it.

对于C#客户端,您可以使用WebRequest检索数据,使用Json.Net对其进行反序列化。

private static TResponse GetResponseValue<TResponse>(WebRequest webRequest)
{
    using (var webResponse = webRequest.GetResponse())
    using (var responseStream = webResponse.GetResponseStream())
    using (var streamReader = new StreamReader(responseStream))
    using (var jsonTextReader = new JsonTextReader(streamReader))
    {
        var jsonSerializer = new JsonSerializer();
        return jsonSerializer.Deserialize<TResponse>(jsonTextReader);
    }
}

#1


1  

For the C# client, you can use WebRequest to retrieve your data, and Json.Net to deserialize it.

对于C#客户端,您可以使用WebRequest检索数据,使用Json.Net对其进行反序列化。

private static TResponse GetResponseValue<TResponse>(WebRequest webRequest)
{
    using (var webResponse = webRequest.GetResponse())
    using (var responseStream = webResponse.GetResponseStream())
    using (var streamReader = new StreamReader(responseStream))
    using (var jsonTextReader = new JsonTextReader(streamReader))
    {
        var jsonSerializer = new JsonSerializer();
        return jsonSerializer.Deserialize<TResponse>(jsonTextReader);
    }
}