【发布时间】:2015-05-28 14:33:59
【问题描述】:
我正在尝试从 c# WPF 桌面应用程序向我的 WebAPI 执行发布。
无论我做什么,我都会得到
{"error":"unsupported_grant_type"}
这是我尝试过的(并且我已经尝试了我能找到的一切):
目前正在测试的还有开发 web api:http://studiodev.biz/
基础 http 客户端对象:
var client = new HttpClient()
client.BaseAddress = new Uri("http://studiodev.biz/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
使用以下发送方法:
var response = await client.PostAsJsonAsync("token", "{'grant_type'='password'&'username'='username'&'password'='password'");
var response = await client.PostAsJsonAsync("token", "grant_type=password&username=username&password=password");
失败后,我做了一些谷歌搜索并尝试:
LoginModel data = new LoginModel(username, password);
string json = JsonConvert.SerializeObject(data);
await client.PostAsync("token", new JsonContent(json));
同样的结果,所以我尝试了:
req.Content = new StringContent(json, Encoding.UTF8, "application/x-www-form-urlencoded");
await client.SendAsync(req).ContinueWith(respTask =>
{
Application.Current.Dispatcher.Invoke(new Action(() => { label.Content = respTask.Result.ToString(); }));
});
注意:我可以使用 Chrome 成功拨打电话。
更新 Fiddler 结果
有人可以帮我成功调用上面的 web api... 如果我能帮助澄清,请告诉我。 谢谢!!
【问题讨论】:
-
您是否尝试过使用 Fiddler 来确定来自 Chrome 的调用与来自 WPF 应用程序的调用之间的区别?
-
我有,请查看更新。我已经尝试了一切来复制结果。请帮忙。
标签: c# wpf asp.net-web-api httpclient token