简体   繁体   English

c# JSON REST 响应通过 3 种不同的方法(WebRequest、RESTSharp、HttpClient)为空,但 Postman 和浏览器有效

[英]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.

相关问题 PostMan GET 有效,但 RestSharp 返回空 JSON 结果 - PostMan GET works, but RestSharp returns empty JSON result GET请求可在HttpWebRequest,Postman中使用。 但不在HttpClient或RestSharp中 - GET request works in HttpWebRequest,Postman . but not in HttpClient or RestSharp HttpClient 后调用适用于邮递员,但不适用于 C# - HttpClient post call works on postman but not C# Http post 在 Postman 中有效,但在带有 HttpClient 的 C# 中无效 - Http post works in Postman but not in C# with HttpClient 在Restsharp中获得与Postman中不同的响应 - Getting different response in Restsharp then in Postman GET 请求在浏览器和 Postman 中有效,但是在使用 HttpClient 和 RestSharp 时我得到了未经授权 - GET request works in browser and Postman, but I get Unauthorized when using HttpClient and RestSharp 403在C#中使用Webrequest的禁止错误,但在邮递员中有效 - 403 Forbidden error using Webrequest in C# but works in postman 在 c# (.net core) 中使用 httpclient 时无法建立 SSL 连接,但可以在 Postman 和浏览器中使用 - The SSL connection could not be established when using httpclient in c# (.net core) but works in Postman and Browser 在 C# 中将 WebRequest 转换为 httpclient - Convert WebRequest into httpclient in C# 无法使用 C# HttpClient 上传文件,Postman 工作正常 - Cannot Upload File using C# HttpClient, Postman works OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM