API无法返回响应

时间:2018-07-31 18:56:58

标签: asp.net-mvc api asp.net-web-api

我正在ASP.NET MVC应用程序项目中实现由gadventures.com制作的api。

我已经实现了以下代码来调用api

enter image description here

IsSuccessStatusCode对于以上代码返回false。它返回401未经授权的错误。但是我能够在邮递员中得到回应。

enter image description here

您能帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

您在邮递员中添加的标头与代码中的标头不同。

代码正在使用方案和参数设置.DefaultRequestHeaders.Authorization,当发送请求时,它看起来像这样。

Authorization X-Application-Key test_21be144dea7e9fa41b5817fe56d2697a2cfc1b20

在邮递员添加通用标头时,发送时看起来像这样

X-Application-Key test_21be144dea7e9fa41b5817fe56d2697a2cfc1b20

更新代码以执行相同的操作。

client.DefaultRequestHeaders.TryAddWithoutValidation("X-Application-Key","test_21be144dea7e9fa41b5817fe56d2697a2cfc1b20");