C# API响应始终为“null”
我创建了一个API和一个登录表单,我需要使用用户名和密码属性授权访问我的API,这是我从登录表单中的两个文本框中获得的。但是,来自API的响应总是空的。这是我的原始代码:C# API响应始终为“null”,c#,api,x-www-form-urlencoded,C#,Api,X Www Form Urlencoded,我创建了一个API和一个登录表单,我需要使用用户名和密码属性授权访问我的API,这是我从登录表单中的两个文本框中获得的。但是,来自API的响应总是空的。这是我的原始代码: public async Task<AuthenticatedUser> Authenticate(string username, string password) { var data = new List<KeyValuePair<string, string>
public async Task<AuthenticatedUser> Authenticate(string username, string password)
{
var data = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username), //I made sure that both username and password
new KeyValuePair<string, string>("password", password) //are passed correctly to the Authenticate() void
};
var content = new FormUrlEncodedContent(data); //var data is not null but "content" is
using (HttpResponseMessage response = await apiClient.PostAsync("/token", content))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<AuthenticatedUser>(); //response is always "null"
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
我尝试用KeyValuePairs数组替换列表,还尝试使用字典。这两种选择都不起作用。在网上做了一些研究之后,我发现了一种使用StringContent或MediaFolder的替代方法,但我不知道如何让它与它们一起工作。
我也在我的域中使用https,所以这里似乎没有错误。目前,FormUrlEncodedContent似乎编码不正确
另外,来自Swagger和Postman的请求返回值。首先,密码授予类型只接受表单编码应用程序/x-www-form-urlencoded,而不接受JSON编码应用程序/JSON
您可以阅读更多关于它的信息,也可以尝试更改以下内容:
替换此项:
var data = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username), //I made sure that both username and password
new KeyValuePair<string, string>("password", password) //are passed correctly to the Authenticate() void
};
var content = new FormUrlEncodedContent(data);
为此:
var content = new FormUrlEncodedContent(
new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password)
}
);
我看到你在Tim Corey的youtube频道《零售经理》上做了教程。我在PostAsync返回null时也遇到了同样的问题 当您将断点设置为line throw new Exceptionresponse.ReasonPhase时,可以检查异常详细信息 在我的例子中,在DataManager项目属性中将SSL endabled设置为True,因此它使用安全协议https打开url。在Tim的教程中,您可以看到http协议 在VS解决方案资源管理器->属性->启用SSL中选择DataManager:False 右键单击DataManager->选择Web选项卡->将项目url更改为:http://... 或选择覆盖应用程序根URL:http://... 检查项目中的App.config文件:VS解决方案资源管理器中的DesktopUI->find tag and change key=api值=http://...
请您删除所有不相关的代码,以提供一个最小的可重复的例子?你说调用FormUrlEncodedContent构造函数后内容为空?是否引发了任何异常?根据您显示的代码,内容不会为null,但是如果您想读取FormUrlEncodedContent实例的内容,您需要使用ReadAsStringAsync方法。它将输出如下内容:grant_type=password&username=user&password=123@ChrisPickford没有例外。和response.IsSuccessfulStatusCode始终为true。如前所述,请将代码缩减为MVCE。请参阅此处:@DanielHalachev若服务器返回成功响应,则表示您已正确发送请求。响应体的问题可能是反序列化失败并返回null。因此,我的建议是将响应简单地读取为字符串,然后调整您的AuthenticatedUser模型以适应它,或者使用带有自定义设置的Json序列化程序。感谢您的响应。我也只尝试了var内容的布局,但它对我也不起作用。另外,我忘记了我使用了Postman和Swagger,它们都从API中提供了响应。根据这些信息,你能想出另一个可能的问题吗?再次感谢你的帮助。我的帖子几乎免费关闭。当从Postman调用api时,您将什么设置为内容类型?您是否将其设置为application/x-www-form-urlencoded当获取令牌时,我在body选项卡中写入了三个键-grant_type、username和password,并使用了x-www-form-urlencoded选项。我希望这就是你问我的请原谅,我对此很陌生。您能设置apiClient.DefaultRequestHeaders.Accept.Addnew MediaTypeWithQualityHeaderValueapplication/x-www-form-urlencoded;而不是apiClient.DefaultRequestHeaders.Accept.Addnew MediaTypeWithQualityHeaderValueapplication/json;响应仍然为空:-