首页 > 解决方案 > 如何从我的控制器向另一台服务器发送请求?

问题描述

我正在尝试从我的控制器向另一台服务器发送请求,但它没有返回结果......什么都没有。

   [Route("api/[controller]")]
    [ApiController]
    public class GetPriceController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public async Task<ActionResult<string>> Get()
        {
var handler = new HttpClientHandler();
            handler.ClientCertificates.Add(new X509Certificate2(@"client.pfx", "password"));

            var HttpCl = new HttpClient(handler);

            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://foobar.com:777")
            {
                Content = new StringContent("data")
            };

            var response = await HttpCl.SendAsync(httpRequestMessage);
            return await response.Content.ReadAsStringAsync();
        }
    }

在 SendAsync 命令之后,什么也没有发生。

   var response = await HttpCl.SendAsync(httpRequestMessage);

标签: c#httpclientasp.net-core-webapi

解决方案


推荐阅读