[英]c# JSON REST response via 3 different approaches (WebRequest, RESTSharp, HttpClient) is empty, but Postman and browser works
When calling a specific endpoint in C# which works without issues in Postman (or via Firefox), I'm getting an empty response.在 C# 中调用在 Postman(或通过 Firefox)中没有问题的特定端点时,我得到一个空响应。
The url I'm calling is returning a collection of data.我正在调用的 url 正在返回数据集合。 In the url parameters I can specify how much of said data I want.
在 url 参数中,我可以指定我想要多少所述数据。 I've inspected the response size in Postman, and when I limit the amount of data requested in my C# call such that the response is around 700kb, then I get a JSON response back.
我已经检查了 Postman 中的响应大小,当我限制 C# 调用中请求的数据量时,响应大约为 700kb,然后我得到一个 JSON 响应。
However, if I exceed this size in the C# call, then the response is empty '{ }' and the ContentLength returned = -1 (the statusCode returned is 200, so this seems fine at least).但是,如果我在 C# 调用中超过此大小,则响应为空 '{ }' 并且返回的 ContentLength = -1(返回的 statusCode 为 200,所以这至少看起来不错)。 This same request which fails in C# works fine within Postman and Firefox however...
在 C# 中失败的相同请求在 Postman 和 Firefox 中工作正常,但是......
I somehow suspect this occurs because either the deserializer's buffer is not big enough OR because the response is still in transit and the code somehow continues executing before it has read the whole response body...我以某种方式怀疑这是因为反序列化器的缓冲区不够大,或者因为响应仍在传输中并且代码在读取整个响应主体之前以某种方式继续执行......
See below for the 3 implementations which I've tested:下面是我测试过的 3 个实现:
1: 1:
var httpClient = new HttpClient();
var responseMessage = await httpClient.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead);
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
using (var httpStream = await responseMessage.Content.ReadAsStreamAsync())
{
using (var sr = new StreamReader(httpStream))
{
Info(await sr.ReadToEndAsync()); //Info logs the string to a file
}
}
}
2 (RESTSharp): 2 (RESTSharp):
var client = new RestClient();
var request = new RestRequest(requestUrl, Method.GET, DataFormat.Json);
Info(request.Content); //Info logs the string to a file
3: 3:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
httpWebRequest.ContentType = "application/json; charset-utf8";
var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
var binReader = new BinaryReader(responseStream);
const int bufferSize = 4096;
byte[] responseBytes;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[bufferSize];
int count;
while ((count = binReader.Read(buffer, 0, buffer.Length)) != 0)
{
ms.Write(buffer, 0, count);
}
responseBytes = ms.ToArray();
}
Info(Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length)); //Info logs the string to a file
I'm not modifying the HttpClient.MaxResponseContentBufferSize property, but for good measure I've also tried changing this value, to no avail.我没有修改 HttpClient.MaxResponseContentBufferSize 属性,但为了更好的衡量,我也尝试过更改此值,但无济于事。
How can I resolve this?我该如何解决这个问题?
I've found the issue, it was being caused by the backend service to which I was connecting.我发现了这个问题,它是由我连接的后端服务引起的。 Thank you again @Panagiotis Kanavos
再次感谢@Panagiotis Kanavos
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.