如何在Web Api中使用带有响应Ok的Httpclient获取对象 [英] How to get object using Httpclient with response Ok in Web Api
本文介绍了如何在Web Api中使用带有响应Ok的Httpclient获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的网络api之类的
public async Task<IHttpActionResult> RegisterUser(User user)
{
//User Implementation here
return Ok(user);
}
我正在使用HTTPClient来请求Web api,如下所述.
I am using HTTPClient to request web api as mentioned below.
var client = new HttpClient();
string json = JsonConvert.SerializeObject(model);
var result = await client.PostAsync( "api/users", new StringContent(json, Encoding.UTF8, "application/json"));
我可以在客户端应用程序上实现的结果请求中找到用户对象吗?
Where i can find user object in my result request which is implemented on client application?
推荐答案
string Baseurl = GetBaseUrl(microService);
string url = "/client-api/api/token";
using (HttpClient client = new HttpClient())`enter code here`
{
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("client_id", "5196810"));
keyValues.Add(new KeyValuePair<string, string>("grant_type", "password"));
keyValues.Add(new KeyValuePair<string, string>("username", "abc.a@gmail.com"));
keyValues.Add(new KeyValuePair<string, string>("password", "Sonata@123"));
keyValues.Add(new KeyValuePair<string, string>("platform", "FRPWeb"));
HttpContent content = new FormUrlEncodedContent(keyValues);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
var result = client.PostAsync(url, content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
}
这篇关于如何在Web Api中使用带有响应Ok的Httpclient获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文