You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Web API调用News API返回400错误求助

调用News API时持续收到400错误的排查求助

我是ASP.NET Core Web API的新手,正在学习调用News API并返回数据,但始终收到400状态码,无法排查代码问题。以下是我的代码:

[ApiController]
[Route("[controller]")]
public class NewsController : Controller
{
     private readonly IConfiguration _configuration;

     public NewsController(IConfiguration iconfig)
     {
          _configuration = iconfig;
     }

     [HttpGet]
     public async Task<IActionResult> GetNews()
     {
          var apiKey = "top-headlines?country=us&apiKey=" + _configuration.GetValue<string>("Keys:NewsAPI");
          var url = "https://newsapi.org/v2/";

          HttpClient client = new HttpClient();
          client.BaseAddress = new Uri(url);

          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

          HttpResponseMessage response = await client.GetAsync(apiKey);

          if (!response.IsSuccessStatusCode)
          {
               return StatusCode((int)response.StatusCode);
          }

          return Ok(response.Content);
     }
}

我确认调试时HttpResponseMessage中的URL看似正确,且参考了Stack Overflow教程及微软文档,但仍毫无头绪。收到的响应体如下:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "Bad Request",
    "status": 400,
    "traceId": "00-fceadd8306e1d45106d330bb98529ae0-9c6374c80d070b7a-00"
}

调试截图:
调试截图

请求协助排查问题。

内容的提问来源于stack exchange,提问作者HeimWinz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 01:10:10