【问题标题】:ASP.Net Core HttpClient from Console returns 503 "Service Unavailable"来自控制台的 ASP.Net Core HttpClient 返回 503“服务不可用”
【发布时间】:2020-02-19 01:33:43
【问题描述】:

问题:通过控制台应用程序访问本地托管的API时,HttpClient返回503“服务不可用”,而使用Edge可以成功访问。我是否缺少一些配置?

代码:

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace SampleClient
{
    public class Program
    {
        private static HttpClient Client = new HttpClient();
        public static async Task Main(string[] args) 
        {
            Console.WriteLine("Starting connections");
            for(int i = 0; i<5; i++)
            {

                var result = await Client.GetAsync("http://localhost:5100/api/contacts");
                //var result = await Client.GetAsync("https://stackoverflow.com/");
                Console.WriteLine(result.StatusCode);
            }
            Console.WriteLine("Connections done");
        }
    }
}

--- 构建---

dotnet 构建

适用于 .NET Core 的 Microsoft (R) Build Engine 版本 16.5.0-preview-20064-06+86d9494e4 版权所有 (C) 微软公司。保留所有权利。

...

构建成功。 0 个警告 0 个错误

样本输出:screenshot

clear headers

【问题讨论】:

  • 在你的api端,请开启debug级别的日志,查看日志是否有错误
  • 如果您尝试在调用GetAsync 之前添加Client.DefaultRequestHeaders.Accept.Clear(); Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); 会怎样?
  • 即使我清除了标题,仍然是错误。
  • @ElendilZheng-MSFT 我认为 API 工作正常。如截图所示,它可以显示在 Edge 中。 (即使在 firefox、chrome 和 postman 中)
  • 在执行 Get to localhost:5100 之前,您是否启动了 .net 核心托管实例或 IIS 网站/应用程序池?

标签: asp.net asp.net-core dotnet-httpclient


【解决方案1】:

对不起。原因是防病毒防火墙。 禁用后就可以访问成功了。

【讨论】:

  • 请问您能具体说明您所做的更改吗?我也面临同样的问题。
猜你喜欢
  • 2021-09-23
  • 1970-01-01
  • 2012-08-15
  • 2014-02-19
  • 2020-05-20
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多