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
相关产品推荐
相关产品推荐

