API 响应总是 "null"
API response is always "null"
我创建了一个 API 和一个登录表单,我需要使用 Username
和 Password
属性授权访问我的 API,我从两个文本框获得在我的登录表单中。但是,API 的响应始终为空。这是我的原始代码:
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);
}
}
}
我尝试用 KeyValuePair<>
数组替换 List<>
,我也尝试使用 Dictionary<string, string>
。这些选项都不起作用。在网上进行一些研究后,我看到了使用 StringContent
或 MediaFolder
的替代方法,但我不知道如何使用它们。
我也在我的域中使用 https,所以那里似乎没有错误。目前看来,FormUrlEncodedContent
编码不正确。
此外,来自 Swagger 和 Postman 的请求 return 值。
首先password
授权类型只接受形式编码application/x-www-form-urlencoded
而不接受JSON编码application/JSON
.
您可以阅读更多相关内容here,也可以尝试更改内容如下:
替换为:
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 频道 Retail Manager 学习了教程。 PostAsync
返回 null
.
我也有同样的问题
在第throw new Exception(response.ReasonPhrase);
行设置断点可以查看异常详情
在我的例子中,它在 DataManager 项目属性中 SSL 已启用 设置为 True,因此它以安全方式打开 url协议-https。在 Tim 的教程中,您会看到 http 协议。
- Select VS 解决方案资源管理器中的 DataManager -> 属性 ->
SSL Enabled: False
- 右键单击 DataManager -> select Web 选项卡 -> 将项目 url 更改为:
http://...
或 select 覆盖应用程序根目录 URL:http://...
- 检查项目中的 App.config 文件:VS 解决方案资源管理器中的 DesktopUI -> 查找标记并更改
key="api" value="http://...
我创建了一个 API 和一个登录表单,我需要使用 Username
和 Password
属性授权访问我的 API,我从两个文本框获得在我的登录表单中。但是,API 的响应始终为空。这是我的原始代码:
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);
}
}
}
我尝试用 KeyValuePair<>
数组替换 List<>
,我也尝试使用 Dictionary<string, string>
。这些选项都不起作用。在网上进行一些研究后,我看到了使用 StringContent
或 MediaFolder
的替代方法,但我不知道如何使用它们。
我也在我的域中使用 https,所以那里似乎没有错误。目前看来,FormUrlEncodedContent
编码不正确。
此外,来自 Swagger 和 Postman 的请求 return 值。
首先password
授权类型只接受形式编码application/x-www-form-urlencoded
而不接受JSON编码application/JSON
.
您可以阅读更多相关内容here,也可以尝试更改内容如下:
替换为:
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 频道 Retail Manager 学习了教程。 PostAsync
返回 null
.
在第throw new Exception(response.ReasonPhrase);
在我的例子中,它在 DataManager 项目属性中 SSL 已启用 设置为 True,因此它以安全方式打开 url协议-https。在 Tim 的教程中,您会看到 http 协议。
- Select VS 解决方案资源管理器中的 DataManager -> 属性 ->
SSL Enabled: False
- 右键单击 DataManager -> select Web 选项卡 -> 将项目 url 更改为:
http://...
或 select 覆盖应用程序根目录 URL:http://...
- 检查项目中的 App.config 文件:VS 解决方案资源管理器中的 DesktopUI -> 查找标记并更改
key="api" value="http://...