在.net core 5应用程序中调用外部api

发布于 2025-01-09 20:50:55 字数 670 浏览 1 评论 0原文

我为公共假期创建了一个 Web API 应用程序。

这是类 Pho

public class Pho
{
    public DateTime date { get; set; }
    public string localName { get; set; }
    public string countryCode { get; set; }
}

这里是我尝试调用 API 的代码

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://date.nager.at/api/v3/PublicHolidays/2017/FR");
    using (HttpResponseMessage response = await client.GetAsync(""))
    {
        var responseContent = response.Content.ReadAsStringAsync().Result;
        response.EnsureSuccessStatusCode();
        return Ok(responseContent);
    }
}

它不起作用,我不知道如何修复它

I created a web API application for public holidays.

Here is the class Pho

public class Pho
{
    public DateTime date { get; set; }
    public string localName { get; set; }
    public string countryCode { get; set; }
}

Here the code I tried to call the API

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://date.nager.at/api/v3/PublicHolidays/2017/FR");
    using (HttpResponseMessage response = await client.GetAsync(""))
    {
        var responseContent = response.Content.ReadAsStringAsync().Result;
        response.EnsureSuccessStatusCode();
        return Ok(responseContent);
    }
}

It didn't work and I didn't know how to fix it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2025-01-16 20:50:55

尝试修复 uri

client.BaseAddress = new Uri("https://date.nager.at"); 
using (HttpResponseMessage response = await client.GetAsync("/api/v3/PublicHolidays/2017/FR"))
{
   if (response.IsSuccessStatusCode)
   {
    var responseContent = await response.Content.ReadAsStringAsync();
     return Ok(responseContent);
    }
return BadRequest("status code "+response.StatusCode.ToString());   
}           
                

try to fix uri

client.BaseAddress = new Uri("https://date.nager.at"); 
using (HttpResponseMessage response = await client.GetAsync("/api/v3/PublicHolidays/2017/FR"))
{
   if (response.IsSuccessStatusCode)
   {
    var responseContent = await response.Content.ReadAsStringAsync();
     return Ok(responseContent);
    }
return BadRequest("status code "+response.StatusCode.ToString());   
}           
                
逆夏时光 2025-01-16 20:50:55

您的代码无法正常工作的可能原因之一是您尝试在客户端尚未初始化时设置 BaseAddress。

要在初始化 HttpClient 时添加 BaseAddress,您应该这样做:

   var client = new HttpClient
            {
                BaseAddress = new Uri("https://date.nager.at/api/v3/PublicHolidays/2017/FR")
            };
            
            using (HttpResponseMessage response = await client.GetAsync(client.BaseAddress))
            {
                    var responseContent = response.Content.ReadAsStringAsync().Result;
                    response.EnsureSuccessStatusCode();
                    return Ok(responseContent);
            }
    

One of the possible reasons your code is not working is because you're trying to set BaseAddress when the client hasn't been initialized yet.

To add BaseAddress at the time of intializing HttpClient you should do it this way:

   var client = new HttpClient
            {
                BaseAddress = new Uri("https://date.nager.at/api/v3/PublicHolidays/2017/FR")
            };
            
            using (HttpResponseMessage response = await client.GetAsync(client.BaseAddress))
            {
                    var responseContent = response.Content.ReadAsStringAsync().Result;
                    response.EnsureSuccessStatusCode();
                    return Ok(responseContent);
            }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文