HttpClient请求抛出了HttpRequestException异常

huangapple go评论62阅读模式
英文:

HttpClient request gives HttpRequestException

问题

这是你的代码部分,我已经将其翻译成中文:

我为我的 Web 应用程序开发了一个健康检查端点。它打印以下 JSON:

{
  "Status": "Healthy",
  "HealthChecks": [
    {
      "Status": "Healthy",
      "Component": "Persistence Server gRPC Status Health Check",
      "Description": "Persistence Server gRPC 状态正常"
    }
  ]
}

这是健康模型类:

internal class HealthModel
{
    public string Status { get; set; } = string.Empty;
    public List<HealthCheck> HealthChecks { get; set; } = new List<HealthCheck>();
}

这是 HealthCheck 类:

internal class HealthCheck
{
    public string Status { get; set; } = string.Empty;
    public string Component { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
}

我有两个使用完全相同模型(HealthModel)的健康检查端点。这两个端点在两个不同的端口上运行。例如:

  • 应用程序1: https://localhost:7001/h-persistence
  • 应用程序2: https://localhost:7101/h-other-persistence

我创建了一个非常简单的 WPF 工具来访问这些 JSON 端点并消耗它们。

这是工具代码:

HttpClient httpClient = new HttpClient();
HealthModel? model = await httpClient.GetFromJsonAsync<HealthModel>(url);
return model;

问题是我的工具可以连接到 https://localhost:7001/h-persistence 并消耗和打印 JSON 内容。但是,当我尝试连接第二个应用程序时,我会收到 HttpRequestException(System.IO.IOException: 响应过早结束)。

我比较了如何初始化这两个应用程序,它们是相同的。我使用相同的 HealthCheck 模型类来读取它们的内容,但我可以成功读取应用程序一的内容,但应用程序二会引发上述异常。

我已经搜索了这个错误,并查看了 Stackoverflow 上现有问题的解决方案,但尚未成功。

到目前为止,我尝试过的方法有:

  • 在 Postman 中输入 URL。收到错误。
  • 输入另一个 JSON 端点的 URL 到我的 HttpClient。我的 HttpClient 正常工作。
  • 将应用程序安装到另一台计算机上。我在 Windows 防火墙中定义了一个规则,允许我的应用程序发送和接收数据包。我得到了相同的错误。
  • 尝试另一个端口号。我得到了相同的错误。
  • 检查是否有其他东西在使用我使用的端口。我没有找到任何东西。
  • 如果我使用互联网浏览器访问 URL,我可以很好地看到 JSON 的内容。

Postman 错误:

错误:read ECONNRESET
请求头
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: c9da74a8-73f1-451f-b145-708dc98d5b72
Host: localhost:7041
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

这是我收到的错误。

英文:

I developed an healthcheck endpoint for my web application. It prints the following JSON:

{
  &quot;Status&quot;: &quot;Healthy&quot;,
  &quot;HealthChecks&quot;: [
  {
    &quot;Status&quot;: &quot;Healthy&quot;,
    &quot;Component&quot;: &quot;Persistence Server gRPC Status Health Check&quot;,
    &quot;Description&quot;: &quot;Persistence Server gRPC status is healthy&quot;
  }]
}

This is the health model class:

internal class HealthModel
{
    public string Status { get; set; } = string.Empty;
    public List&lt;HealthCheck&gt; HealthChecks { get; set; } = new List&lt;HealthCheck&gt;();
}

This is HealthCheck class:

internal class HealthCheck
{
    public string Status { get; set; } = string.Empty;
    public string Component { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
}

I have 2 health-check endpoints that uses the exact same model (HealthModel). Those 2 endpoints are running over 2 different ports. For example;

  • application1: https://localhost:7001/h-persistence
  • application2: https://localhost:7101/h-other-persistence

I created a very simple tool with WPF to access those JSON endpoints and consume them.

This is the tool code:

HttpClient httpClient = new HttpClient();
HealthModel? model = await httpClient.GetFromJsonAsync&lt;HealthModel&gt;(url);
return model;

The problem is my tool can connect to https://localhost:7001/h-persistence and consume and print the json content. however, I get HttpRequestException (System.IO.IOException: The response ended prematurely.) when i try to connect the 2nd application.

I compare how i initialize two application and they are identical. I use the same HealthCheck model classes to read their contents but I can read app one's content successfully but app two throws the exception above.

I searched the error already and looked at the solutions in the existing questions on Stackoverflow but no success yet.

Here is what I have tried so far:

  • I entered url into Postman. Received error.
  • I entered another json endpoint url to my HttpClient. My HttpClient works fine.
  • I installed the application on another computer. I defined a rule in Windows Firewall that allows my application to send and receive packets. I got the same error.
  • I tried another port number. I got the same error.
  • I checked if anything else is using the port that I use. I didn't find anything.
  • I can see the content of the json very well, if i visit the URL with an internet browser.

Postman Error:

Error: read ECONNRESET
Request Headers
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: c9da74a8-73f1-451f-b145-708dc98d5b72
Host: localhost:7041
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

Here is the error I get:

HttpClient请求抛出了HttpRequestException异常

答案1

得分: 1

以下是翻译好的部分:

"The information you're showing indicates that there is an error happening on the server, not the client.

Check 1) the IIS logs to see if it lists an error code, 2) your NT Event logs ("Application" log) for any unhandled exceptions, 3) any (server-side) error handling & logging?"

英文:

The information you're showing indicates that there is an error happening on the server, not the client.

Check 1) the IIS logs to see if it lists an error code, 2) your NT Event logs ("Application" log) for any unhandled exceptions, 3) any (server-side) error handling & logging?

huangapple
  • 本文由 发表于 2023年4月6日 20:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定