【问题标题】:HttpClient on ASP.NET Core application times out when connecting to service on localhost连接到本地主机上的服务时,ASP.NET Core 应用程序上的 HttpClient 超时
【发布时间】:2019-01-10 19:06:17
【问题描述】:

我有一个作为 .NET 控制台应用程序运行的 XML-RPC 服务器(使用 XML-RPC.net)。我正在尝试通过我的 ASP.NET Core (2.1.1) Web 应用程序连接到它,但客户端一直超时。 Postman 也会立即返回响应而不会出现问题。

我是这样称呼它的:

        HttpClient client = _clientFactory.CreateClient();

        client.Timeout = TimeSpan.FromSeconds(10);

        var httpRequest = new HttpRequestMessage(HttpMethod.Post, instance.ServiceUrl);
        var stringContent = new ByteArrayContent(Encoding.UTF8.GetBytes(request.ToString()));

        httpRequest.Content = stringContent;
        httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");

        var httpResponse = await client.SendAsync(httpRequest);
        var response = await httpResponse.Content.ReadAsByteArrayAsync();

当控制台应用返回响应时,我可以看到请求已成功发出。 Fiddler 显示有 200 个响应,但 await client.SendAsync(httpRequest); 超时!

请求通常在 10 毫秒内完成,因此超时值仅用于调试,如果我将其省略,则需要 60 秒。响应返回 XML。

我尝试重写它以使用 StringContent 和使用 PostAsync,同样的问题。我还尝试使用 WebClient 重写它,但它返回 The remote server returned an error: (100) Continue. 不确定这是否相关。

暂时卡在这个问题上,有人知道会发生什么吗?

【问题讨论】:

    标签: asp.net .net .net-core xml-rpc.net


    【解决方案1】:

    好的,我做了一些谷歌搜索,看起来我需要这条线:

    client.DefaultRequestHeaders.ExpectContinue = true;
    

    肯定是和100状态码返回没有处理好有关。

    在这里找到它: https://social.msdn.microsoft.com/Forums/en-US/042016f0-d70e-42f9-9924-5febeb2bea86/excluding-the-quotexpect-100continuequot-header-from-httpwebrequest-posts?forum=winappswithcsharp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多