Consume web API in MVC or Windows Application or Console Application
Sometime, we need to consume web API mostly in MVC or Windows Service or Windows Application or Console Application.Below code posts dynamic object in API call,
var url = "http://localhost/DemoWebAPIProject/api/ControllerName/Method";
Uri requestUri = new Uri(url);
dynamic dynamicObject = new ExpandoObject();
dynamicObject.UserName = "anorathod".ToString();
dynamicObject.Password = "password";
string json = "";
json = Newtonsoft.Json.JsonConvert.SerializeObject(dynamicObject);
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.PostAsync(requestUri, new StringContent(json, System.Text.Encoding.UTF8, "application/json"));
string responJsonText = await response.Content.ReadAsStringAsync()
I hope that this will helpful in your project.
Enjoy !!!
:)
No comments:
Post a Comment